VISHNU VARDHAN REDDY UNDYALA said:
The value of pi is already in a float ... and the output comes till
like 6-7 decimal points. I want the value of pi till 2 decimal points
with out getting round off ... and which could be assigned to another
variable (any). Thanks
Don't assume that your readers are able to see the article to which
you're replying.
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Please complain to Google about their broken interface.
What exactly do you mean by "without getting round off"? Do you mean
that you want the result truncated rather than rounded, so for 4
decimal placed you get 3.1415 rather than 3.1416?
As Barry Schwarz already told you, what you're asking for is basically
not possible. Given a floating-point value approximating pi, you can
obtain a value that *approximates* the value of pi truncated to 2
decimal places (3.14), but the exact value 3.14 cannot be represented
in binary floating-point.
Here's a program that demonstrates the problem. (Note that I used
double rather than float.)
#include <math.h>
#include <stdio.h>
int main(void)
{
double pi = 4.0 * atan(1.0);
double pi2 = floor(pi * 100.0) / 100.0;
double pi4 = floor(pi * 10000.0) / 10000.0;
printf("pi = %f, really %.20f\n", pi, pi);
printf("pi2 = %f, really %.20f\n", pi2, pi2);
printf("pi4 = %f, really %.20f\n", pi4, pi4);
return 0;
}
And here's the output I got (it may be slightly different on your
system):
pi = 3.141593, really 3.14159265358979311600
pi2 = 3.140000, really 3.14000000000000012434
pi4 = 3.141500, really 3.14150000000000018119
Presumably truncating the value like this is a means to an end rather
than an end in itself. What exactly are you trying to accomplish?