This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Closed 5 years ago.
int a=pow(100,2);//line 1
int b=ceil(pow(100,2));//line 2
cout<<pow(100,2);//line 3
line 1 gives a=9999 on printing the value of a
line 2 gives b=10000 on printing value of b
line 3 prints 10000
I understood that pow give value 9999.9999 so ceil func. in line 2 gives it the upper value.
But why didn't cout print 9999.9999
Can anyone explain why pow behave like this return decimal value pow is just a power function why doesn't it simply give 100*100 as answer?
In the cout statement, pow(100, 2) is indeed a double-precision value and slightly below 10000 (by a well-known effect of the pow function), but the default accuracy setting of cout causes rounding and output as an integer.
The reason is due to pow taking two double values as arguments (and returning a double) and is typically implemented such that pow(x, y) is exp(y log x). This "goes off" for even seemingly trivial input values. See Is floating point math broken?
Note that std::pow has overloads for integral types which can be helpful when working in integer arithmetic.
Related
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 3 years ago.
I am trying to store coordinate points in a double variable. It is very simple. I have x and y coordinates. My double variable stores them like that x.y . When I tried to convert this value into separate coordinates, I had some troubles.
I have tried these codes but still get same error.
//First try
double temp=memory.pop();
int x=(int)temp;
int y=(int)((temp-(int)temp)*100);
//Second try
double temp=memory.pop();
int x=(int)temp;
int y=100.0f*temp-(((int)temp)*100.0f);
In temp variable, I have 5.14 double number. After calculations, x should be 5 and y should be 14. However, x become 5, y become 13.
The issue is related to integer casting. Specfically (int)temp will floor to the first smaller integer. For insance, the value 5,14 can be (and probably is) not exaclty that but a bit smaller/larger for a matter of precision (since, as said in comments, the floating point numbers don't have infinite precision). So without loss of generality let's say that is 5,13999999999999, you can see that when you perform your operation you will obtain 13 instead of 14 for the second part of the coordinate.
This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
This is really basic question, I'm testing std::cout to print float's, the following works correctly:
std::cout<<10 / 3.2<<std::endl; //Output: 3.125
But when I try:
std::cout<< 500000 / 1000000<<std::endl; //Output: 0
Why is the output of my second example not 0.5? Is this automatically rounding down?
I'm compiling with g++ -std=c++14
In your second code example 500000 and 1000000 both have integer type, so, when you divide one to another, you have integer division (the result is rounded bottom), which is 0. To fix this, try multiplying them by 1.0 or casting to any floating-point type.
The compile time evaluable constant expression 500000 / 1000000 will be evaluated in integer arithmetic, which effectively means that any remainder is discarded. (Note that / has higher precedence than << so the division is evaluated first).
A clear rememdy is to promote one of the arguments to floating point (the other one is then promoted automatically by the compiler). I always pick the first one to signal to the reader of your code that you know what you're doing:
500000.0 / 1000000
This question already has answers here:
Division not outputting correct answer c++
(2 answers)
Closed 7 years ago.
I'm currently working on a C++ program where I have a bankAccount class and I need to calculate interest. Problem is, my function is rounding off my numbers to the whole number, even though I'm using a float as my variable type. So if I had code like this:
float BankAccount::CalculateInterest(int Time)
{
float thing;
thing = (967 / 365);
return thing;
}
thing ends up equaling 2.00000000, when it should equal 2.649315068.
Any ideas? I had my interest equation in there before, and the result was always 1.00000000 no matter what, so I put this in as a test and I saw that it's rounding to the "floor".
Thanks for your help!
It's not rounding.
You are dividing two integers and the result will be an integer.
You can remedy this by simply adding ".0" to both numbers, or explicitly defining them as floats.
967 and 365 are integers, so integer division is used and the remainder discarded. Make one a floating point number and floating point division will be done: 967 / 365.0.
Alternatively:
float thing = 967; // integer converted to float during assignment
thing /= 365; // division of float and converted integer
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 8 years ago.
Now I understand floats are less accurate than double, but does this explain when I have the std::string:
"7.6317"
and I do:
float x = atof(myString.c_str());
getting 7.63170004 is expected? Is there any way I can tell the assignment of x to only read the first 4 decimal places? Or is this because of the way the float representation stores the number 7.6317?
Yes. It is expected. It is so-called floating point error.
Some floating point literals do not have an accurate representation in the computer, even if -- in decimal notation -- the number seems harmless. This is because the computer uses 2 as a base. So even if a number might have a finite representation in base 10, it might not have on in base 2.
you can do it like:
float x = floorf(val * 10000) / 10000;
i think it should work! see See
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
Why when I use multiplication
float a= 1.0500000f;
float b= a*100.0f;
why b is 104.99999 but not 105.0 ?
and when I
int f= (int)b;
f is 104
Floating point numbers are not infinitely accurate -- the wikipedia page on Float numbers is quite interesting to read.
On your second question: (int)b truncates anything that occurs after the comma. This means that, in your case, 104.99999 becomes 104. So when you create a rounding error, and then cast it to an integer, you are indeed running the risk of getting a lower number.