Unexpected behaviour of int() [duplicate] - coldfusion

This question already has answers here:
Why does this subtraction not equal zero?
(7 answers)
Closed 6 years ago.
I'm a bit confused about following behaviour of the int()-function in ColdFusion 10 (10,0,20,282462):
<cfset dummy = 100 - (5859 / (6510 / 100)) />
<cfoutput>
dummy = #dummy#<br><br> <!--- 10 --->
int(10) = #int(10)#<br> <!--- 10 --->
int(dummy) = #int(dummy)# <!--- 9 --->
</cfoutput>
Can anybody explain me why int(dummy) returns 9 instead of 10?

int(dummy) returns 9 instead of 10 because it is essentially floor() in other languages, and your answer may become 9 because for performance, by default, they are treated as double.
Have you heard of PrecisionEvaluate() https://cfdocs.org/precisionevaluate
dummy = PrecisionEvaluate(100 - (5859 / (6510 / 100)));
writeOutput(dummy);
you'll get 10 as expected

Related

Getting a weird result [duplicate]

This question already has answers here:
Display a number decimal format instead as an exponential in cout
(2 answers)
Closed 2 years ago.
float k,n,s,p;
float a,d;
cin>>k>>n>>s>>p;
a=n/s;
a=ceil(a);
d=k*a;
d=d/p;
cout<<ceil(d);
Am getting an output = 1e+008 when the inputs are 10000, 10000, 1, 1.
The actual result should be 100000000.
I tried using double instead of float but no success.
Kindly throw some light
Use std::fixed
cout<<fixed<<setprecision(8)<<d<<endl;

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

C++ Random doubles from -1 to 1 [duplicate]

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;
You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

C++ Division Error? [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
Hi so whenever I try to do division such as double x = 3 * (5/10); it will make x = 0 for some reason. Is there a reason this happens in c++ I'm learning and have no clue why this happens.
think about this: what data type is 5?
what data type is 10?
INTEGER!!!
then
(int)5 / (int)10 = (int)0.5 = 0
try this
double a = 3;
double b = 5;
double c = 10;
double x = a * (b/c);

How to Set certain digits for generating random numbers in C++ [duplicate]

This question already has answers here:
How do I scale down numbers from rand()?
(9 answers)
Closed 8 years ago.
Hi. Is there any way to set the size of random numbers?( in random number generator "rand()")
For example I want to generate 10 digits random numbers.
and one more question, how can i set random function to generate numbers between 0 and 1 (for example 0100110110) ?
Im not sure about setting the size of the numbers. However I dont think it would be possible to get each digit to produce just a 0 or 1.
What you can do however is something like below:
ostringstream 10digitNumber;
for(int i = 0 ; i < 10 ; i ++){
v1 = rand() % 2;// generate o or 1
10digitNumber<< v1;// build up a string of 1 and 0
}
int real10DigitNumber = static_cast<int>10digitNumber); // typecast to integer
Please forgive me if my syntax isn't 100 %. Its being awhile since I used c++.