This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Division in C++ not working as expected
Turns out my program has been returning wrong results, so I decided to break the code into little pieces. After setting breakpoint, turns out that...
double test3 = ((2 - 1) / 2);
...equals 0 according to C++ compiler. I have no idea why. Can someone explain it to me?
I'm using MS Visual Studio Premium 2012
Because you are doing integer division. 1/2 is 0, which is then converted to double, yielding 0.. If you want floating point division, try making one of the arguments of the division a floating point number:
double test3 = (2.0-1)/2;
Because the numbers you used on the right hand side are all integers: (i.e.: the expression (2-1)/2 evaluates to 0 as (int)1/(int)2 evaluates to 0 since the whole thing is an integer.
Change it to:
double test3 = ((2 - 1) / 2.0);
And the expression is then (int)1/(double)2, which will evaluate to a double, and thus 0.5
When only integers are involved in an expression, you will only get integer arithmetic. If you want to have floating point arithmetic, you need to involve a floating point expression at some point, e.g.
double test3 = ((2 - 1) / 2.0);
try making your numbers double, (2.-1.)/2.;
Related
This question already has answers here:
Fastest way to get a positive modulo in C/C++
(9 answers)
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 2 years ago.
In a code written by me, I have used both below functions to calculate mod of displayed negative number.
fmod(-10,11)
(-10, 11)
Though the correct answer is 1. It always displays the answer -10 in c++. How I can solve it?
From cppreference.com:
double fmod (double numer, double denom);
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.
In your case it is -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10, which is correct for that implementation of fmod. If you need another answer, you should implement your own version, as modulo is defined in different ways for negative numbers.
This question already has answers here:
Rounding up and down a number C++
(5 answers)
Closed 5 years ago.
I think truncating can be done by converting float to int
e.g 25.83f will be 25 when converted to int
but it should be 26 when rounded off..how this can be achieved
please help me thanks
Similarly I think ceiling can be done by converting to int and then adding 1 to it..is it true??
Without using round, floor, ceil etc. (without "using build in functions") you can round by simply adding 0.5 and truncating to int:
float x = 25.83f
int y = (int)(x + 0.5f);
But beware of negative numbers, there you might want to do the opposite, i.e. subtract:
float x = -25.83f
int y = (int)(x - 0.5f);
Nevertheless, this is what the build in functions are designed to do, so I don't see many valid reasons to avoid them.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have a weird problem. Here's part of my code:
int temp=1100;
int foo=floor(0.03*temp);
int foo1=0.03*temp;
if(foo-foo1){
cout<<foo<<endl;
cout<<foo1<<endl;
}
If 3% of temp = integer then foo differs from foo1 by 1.
For example: 1100*0.03=33. foo=33 foo1=32.
In addition if i write it like that :
int foo=floor(0.03*1100);
int foo1=0.03*1100;
There is no such problem.
Why?
Floating point numbers have trouble rendering decimal places. It is more of an approximation.
2**-6 + 2**-7 + 2**-8 + 2**-9 + 2**-11 + 2**-13 + 2**-14 + 2**-15 + 2**-20 + 2**-22 + 2**-26
= 0.0299999863
Using binary, I drove the accuracy to 26 binary. I got close to 0.03 but not quite. In my example I chose to be under 0.03 but I could have gone a little above (i.e. 0.03000001. I don't think it is possible to represent 0.03 perfectly in floating point notation.
Multiplying 0.03 by any number produces yet another approximation. Casting to type int will cut out everything after the decimal place. I assume the implementation of floor is more elegant. Your compiler probably choose a floating point value of 32.99999 so and int would make 32.
This question already has answers here:
Error subtracting floating point numbers when passing through 0.0
(4 answers)
Closed 9 years ago.
Consider the following code snippet:
float f = 0.01 ;
printf("%f\n",f - 0.01);
if (f - 0.01 == 0)
{
printf("%f\n",f - 0.01);
}
When I run this code, for the second line I get the output -0.000000, and the if condition does not execute .
What is the reason for the -0.000000?
I remember from a digital logic class I took in college that this arises due to internal representation using one's complement. Please correct me if I'm wrong and please suggest fixes and how to avoid this in the future .
I'm using clang to compile my code , if it matters.
You're running into two problems:
0.01 can't be represented exactly as a binary floating-point value
f has type float while 0.01 has type double
Your calculation requires a conversion from double to float and back which (apparently) isn't giving exactly the same value that it started with.
You might be able to fix this specific example by sticking to a single type (float or double) for all values; but you'll still have problems if you want to compare the results of more complicated calculations for exact equality.
0.01 is a double not a float (You probably have warnings about this when you compile your code.)
So, you're basically converting your "0.01" backwards and forwards between floats and doubles, which is what's causing your discrepancies.
So decide if you want to use floats (e.g 0.01f) or doubles, and stick with one version throughout.
However, as other answers have pointed out, you'll never get an "exact" value when doing floating point arithmetic - it just doesn't work that way.
For reference, both of these versions will give the answer you're expecting
float f = 0.01f ;
printf("%f\n",f - 0.01f);
if (f - 0.01f == 0)
{
printf("%f\n",f - 0.01f);
}
or
double f = 0.01 ;
printf("%f\n",f - 0.01);
if (f - 0.01 == 0)
{
printf("%f\n",f - 0.01);
}
both print
0.000000
0.000000
The reason is that, 0.01 can't be represented correctly in binary floating number. This can be understand by an example: 10/3 is giving the result 3.333333333333333......, i.e it cant be represented correctly in decimal. Similar case with 0.01. Every floating point decimal number can't be represented correctly in binary floating point equivalent.
You have two double 0.01 - one is converted to a float. Due to loss of precision double(float(0.01)) != double(0.01)
Even without the obvious loss of precision you might get into trouble using double(s) only. A compiler might keep one as an extended double in a register and fetch the other from memory (stored as double)
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 9 years ago.
I am trying to do a simple calculation : ( (45/100) - (20+50)/200 )*(10+5)
I am expecting the answer to be 1.5 but when the programme is compiled , it shows 0.
Can someone figure this out for me
#include <iostream>
using namespace std;
int main()
{
float CivIndex = ( (45/100) - (20+50)/200 )
*(10+5);
cout<<CivIndex; // expect 1.5 but getting 0
}
Integer division!
(45 / 100) equals 0 when evaluated as an integer, not 0.45 as you'd been hoping.
Make either numerator or denominator a float to get the expected result:
(45.0 / 100)
What you are doing is integer division, and integer division rounds the result to the closest integer. To correct your code, change it to:
#include <iostream>
using namespace std;
int main()
{
float CivIndex = ( (45.0/100.0) - (20.0+50.0)/200.0 )
*(10.0+5.0);
cout<<CivIndex; // expect 1.5 but getting 0
}
Note: not all .0 are needed, but just put them to be sure.
You are doing integer division.
Specify it as float constants
float CivIndex = ( (45./100) - (20+50)/200. )*(10+5);
^ Notice decimal points^
All your constants are ints, therefore, the compiler is doing integer math. 45/100 as an int is 0. So is 70/200. 0 - 0*15 = 0. You need to tell the compiler that your constants are floats: 20f, or 20.0 would both work. (For each operation, if at least one constant is a float, the operation will be treated as floating point math.)
In C and several other languages diving two integers result in an integer (integral division). That is 45 / 100 in your code will result in 0 instead of the expected 0.45
The fix is simple: convert one of the operands to floating point.
float CivIndex = ( (45/100.0) - (20+50)/200.0 )
*(10+5);
You are hoping the integer division as 0.45 but that is actually 0
Try to change this as:
float CivIndex = ( (45.0/100) - (20.0+50.0)/200 )
*(10+5);
You are essentially evaluating an expression containing only integers. So the
result will be an integer.
You can use casts on the final result of the integer expression.
e.g..
int a=20;float b; b=(float)a;cout<<"b: "<<b;
Please confirm the syntax.
Or as stated above, you can also make one of you operands as a float/double(if your requirement permits).