using printf to print a floating number with varying precision [duplicate] - c

This question already has answers here:
Printf variable number of decimals in float
(3 answers)
Closed 6 years ago.
A simple question.
I want to print a floating point number with precision given input from the user, i.e. for num=2.34567 and prec=2, I should print 2.35 as the answer, and for prec=3, I should print 2.346. How can we achieve this? (prec is given input from the user during runtime).
Thanks in advance.

This is probably what you are looking for:
float num = 2.34567;
int prec = 3;
printf("%.*f", prec, num);

Related

wrong decimal values in c? [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
i want to extract the decimal part of a float variable by substracting the whole part, the thing is i get a wrong value
#include<stdio.h>
int main(){
int k ;
float a=12.36,i;
k = (int)a;
i = a - k ;
i*=10;
printf("%f",i);
return 0;
}
well, the output is 3.599997 not 3.6 , is there a way to solve this ?
edit : i know it's because of the binary conversion, i m asking if there is a solution to get the right result, not the cause of it. ty for the replies anw.
edit 2 : sadly it's not a matter of display, i want the stored value to be 3.6 ( in this case) because i need it in other calculations.

What is wrong with this expression on the code? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);

How to compare a float using greater than and less than (> and <) in C [duplicate]

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>

adding numbers in a for loop [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I tried to sum some numbers in a for loop but it didn't go as I expected
float sum = 0;
int i;
printf("0.1+0.1=%f\n", 0.1 + 0.1);
for (i = 0; i<1000000; i++)
{
sum = sum + 0.1;
}
printf("the sum need to be 100000 \n");
printf("the real sum is:\n %f\n", sum);
system("PAUSE");
this program prints:
0.1+0.1=0.200000
the sum need to be 100000
the real sum is:
100958.343750
Press any key to continue . . .
can you explain please this strange result?
the international standard for floating point numbers does not have an exact representation for some decimal numbers.
http://en.wikipedia.org/wiki/IEEE_754
It is due to the way they are stored in memory, the way the mantissa and exponent are stored.
https://en.wikipedia.org/wiki/Floating_point
This is also the reason why you should never compare two float numbers even if they look "the same".
I still remember how surprised I was the fist time a simple code comparing two float numbers didn't work :) This alone would open a dedicated universe of discussions. It is very worth reading anyway:
http://floating-point-gui.de/errors/comparison/
The floating numbers are stored in memory as x*2^y where x is between 0 and 1 with some precision and y is integer and so they accurately don't represent most numbers, they represent numbers "close enough".
When you do this addition multiple times, the error is just more visible.
You can use double type for better accuracy.

Proper Decimal Problem? [duplicate]

This question already has answers here:
Two decimal places using printf( )
(4 answers)
Closed 8 years ago.
#include <stdio.h>
int main (){
float M=2E30, G=6.67E-11, m=6E24, r=1.5E11;
float F= (G*M*m)/(r*r);
printf("F is %f",F);
return 0;
}
I am trying to print the value of F with two decimal precision. Could anyone help me please?
For two decimal precision, change your printf statement as below
printf("F is %.2f", F);
read this for more information.
You should try this:
printf("F is %0.2f",F);
The 0 in %0.2f tells that the output need not to be right justified and the 2 after the decimal tells that there should be only 2 digits after the decimal in the output. Basically %0.nf allows us to set a precision of n digits after the decimal in the output.
Hope this helps.

Resources