This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
The following piece of code :
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7, a);
return 0;
}
gives 0.7000000000 0.6999999881 as answer while the following piece of code :
int main()
{
float a=1.7;
printf("%.10f %.10f\n",1.7, a);
return 0;
}
gives 1.7000000000 1.7000000477 as output.
Why is that in the first case upon printing a I got a value less than 0.7 and in the second case more than 1.7?
When you pass a floating-point constant to printf, it is passed as a double.
So it is not the same as passing a float variable with "the same" value to printf.
Change this constant value to 0.7f (or 1.7f), and you'll get the same results.
Alternatively, change float a to double a, and you'll also get the same results.
Option #1:
double a = 0.7;
printf("%.10f %.10f\n",0.7,a);
// Passing two double values to printf
Option #2:
float a = 0.7;
printf("%.10f %.10f\n",0.7f,a);
// Expanding two float values to double values and passing them to printf
Related
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
For float value the output is coming"okNOT OK", It should have shown "okOK". But with other datatypes it is working fine. Why is it hapenning?
#include<stdio.h>
int main()
{
float a=0.5, b=0.7;
if(a==0.5)
printf("ok");
else
printf("not ok");
if(b==0.7)
printf("OK");
else
printf("NOT OK");
return 0;
}
By default, decimal values are double, which means you compare 0.7f (float) with 0.7 (double). These values are different, as double uses more bits to save data about the number itself. To fix your problem, change your if statements to:
if(a == 0.5f)
...
if(b == 0.7f)
...
This way you will compare float a, b with corresponding float values.
This question already has answers here:
How dangerous is it to compare floating point values?
(12 answers)
Closed 3 years ago.
The value of january13.overtime_hours is 0.000000 (it's a float)
Can someone please explain to me why the code below still executes the code inside the if statement? However if I try to put == instead of ">" it doesn't execute.
printf("%f", january13.overtime_hours);
if (january13.overtime_hours > 0.0)
{
overtime_paycheck = ( january13.overtime_rate *
january13.overtime_hours);
printf("Overtime Pay: %.2f\n", overtime_paycheck);
}
Learn about DBL_EPSILON/FLT_EPSILON, these are macro from cfloat.
So what are these?
difference between 1.0 and the next representable value for float, double and long double respectively
To compare them with ==, you have to write fabs(a-b)<=FLT_EPSILON, it compares if two number a and b are equal or not, if this statement returns true, then they have no difference between them actually.
Use function based on your need, ref :
float fabsf( float arg );
double fabs( double arg );
long double fabsl( long double arg ); // Defined in header <tgmath.h>
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 4 years ago.
I am new to C language. Here is my question and code:
I have a constant and a variable with the same value and I try to compare them to check if they are equal. It seems to me that since they are assigned the same value, they should be equal but that is not the case.
#include <stdio.h>
#define mypi1 3.14
int main()
{
int ans;
float mypi2 = 3.14;
if(mypi1==mypi2)
{
ans=1;
}
else
{
ans=0;
}
printf("%i",ans);
return 0;
}
My output is 0. Which indicates they are not equal. What is the reasoning behind this? This is a really simple question but I could not find it anywhere. Please help and thanks in advance.
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double type, the second is a double coerced into a float.
The expression mypi1==mypi2 will first convert the float back to a double before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if statement is failing, it's likely that you lose information in the double -> float -> double round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double everywhere. If you do need float types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50f\n", x);
printf("%.50f\n", z);
}
In this, x is a double and z is a double that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
#include<stdio.h>
void main()
{
float a = 2.3;
if(a == 2.3) {
pritnf("hello");
}
else {
printf("hi");
}
}
It prints "hi" in output, or we can say that if condition is getting false value.
#include<stdio.h>
void main()
{
float a = 2.5;
if(a == 2.5)
printf("Hello");
else
printf("Hi");
}
It prints hello.
The variable a is a float that holds some value close to the mathematical value 2.3.
The literal 2.3 is a double that also holds some value close to the mathematical value 2.3, but because double has greater precision than float, this may be a different value from the value of a. Both float and double can only represent a finite number of values, so there are necessarily mathematical real numbers that cannot be represented exactly by either of those two types.
In the comparison a == 2.3, the left operand is promoted from float to double. This promotion is exact and preserves the value (as all promotions do), but as discussed above, that value may be a different one from that of the 2.3 literal.
To make a comparison between floats, you can use an appropriate float literal:
assert(a == 2.3f);
// ^
2.3 with binary representation is 01000000000100110011001100110011...
so you are not able to set a float exactly to 2.3
with double precision you get something similar: 2.299999952316284
you converted a double to float when you wrote:
float a = 2.3;
the if checks if the float a is equal to double 2.299999952316284
you should write:
float a = 2.3f;
and you can check:
if (a == 2.3f) {
...
}
i would rather test with:
if (fabs(a - 2.3f) < 0.00001) {
...
}
the 2.5 represented with bits is: 01000000001000000000000000000000
EDIT: fabs is part of the <math.h> or <cmath>
Read this: article
Comparing floating point values is not as easy as it might seem, have a look at Most effective way for float and double comparison.
It all boils down to the fact, that floating point numbers are not exact (well
most are not). Usually you compare 2 floats by allowing a small error window (epsilon):
if( fabs(a - 2.3f) < epsion) { ... }
where epsilon is small enough for your calculation, but not too small (bigger than Machine epsilon).
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 7 years ago.
I've written a code for the following program but the output seems to be wrong.
Question:
https://www.hackerrank.com/challenges/plus-minus
Code:
#include <stdio.h>
int main() {
int N,num,i,cp=0,cn=0,cz=0;
double fp,fn,fz;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&num);
if(num>0)
cp=cp+1;
else if(num==0)
cz=cz+1;
else
cn=cn+1;
}
fp=cp/N;
fn=cn/N;
fz=cz/N;
printf("%lf\n%lf\n%lf",fp,fn,fz);
return 0;
}
The Output comes as:
0.000000
0.000000
0.000000
Istructions:
fp=cp/N;
fn=cn/N;
fz=cz/N;
Are performed as integer division.
Change your code to:
fp=(double)(cp)/(double)(N);
fn=(double)(cn)/(double)(N);
fz=(double)(cz)/(double)(N);
you are doing an integer division which creates only integer results. If you want to calculate floating point results you need to perform floating point divisions.
int a = 1;
int b = 3;
int c = a / b;
// c is now 0 -> integer division
double i = 1.0;
double j = 3.0;
double k = i / j;
// k is now 0.3333333 -> floating point division
For correct result cast these expression's to double-
like this
fp=cp/N; // fp=(double)cp/N;
fn=cn/N; // fn=(double)cn/N;
fz=cz/N; // fz=(double)cz/N;
Working code
In previous case if cp(or cn or cz) is less than N then due to integer division you will get 0(fraction part will be discarded). Therefore m these casts .
Another method would be to use all variables as double .