1. ## Float is truncating?!

Alright well I'm supposed to make a program that does a few calculations and prints the results but I'm having a problem with the float truncating the answer.
What I have essentialy is (Simplified version):

float = test;
test = 34*pow(10,9);

but instead of test being equal to:
34000000000

it's equal to:
33999998976

Is there any way of fixing this? I tried double which solves the problem however I need to print it as a number without exponents and I can't seem to figure out how to convert double to a float. 2. All floating point types have finite precision - meaning there are values that cannot be represented exactly. It is therefore always possible (even with double) to find values that will be "truncated" as you describe.

It is a fundamental property of floating point representations so, no, you can't fix it in general. Any fix you do so 34*pow(10,9) is printed as 34000000000 rather than as 33999998976 will simply fail to behave as intended for some other set of floating point values. It's like poking a pillow: smooth a bump on one part of the pillow, and a bump appears elsewhere.

Converting a double to a float (which can be done as float_value = (float)double_value) will not fix the problem, as converting from a double to a float loses precision. There are values a double can represent exactly that a float cannot.

If you want to understand, there are many articles and textbooks on numerical analysis (and floating point operations) that describe this phenomenon in detail. Bear in mind that numerical analysis is a very large field. And this basic property of floating point types is the reason the field of numerical analysis exists at all. 3. Well then is there a way of storing large numbers without them truncating? Int won't store such a large number, double does however I need it to print the numerical value instead of printing "3.4e^10" 4. Write a little function that takes a string "3.4E+10" (the floating point form) and produces "34000000000". The logic will not be difficult.

Just use sprintf() to produce the first string, and then output the result of your function using fprintf() (or whatever technique you prefer). 5. Code:
```#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
long double x = 34*pow(10,9);

printf("%.0Lf\n", x);

return EXIT_SUCCESS;
}```
This works fine here. 6. Alright well that worked Only one more question is there a way of evaluating if the double is less than another number? Because what I have currently doesn't work. for example:
Code:
```double test = 34*pow(10,9);
if(test < pow(10,12))
{
printf("%f", test/pow(10,9));
}``` 7. What do you mean it doesn't work? Is that missing bracket that should be enclosing if a typo? Otherwise, it should work. 8. Sorry the missing bracket is actually there in the code :P Just forgot it on the forums. But it doesn't seem to be printing anything. I have a printf() before and printf() after the if statement it prints the first one but not the second one. 9. Then it's something else wrong with your code. It works here.

Code:
```test\$ cat dftest.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
double test = 34*pow(10,9);
if(test < pow(10,12))
{
printf("%f", test/pow(10,9));
}

return EXIT_SUCCESS;
}
test\$ gcc -O3 -Wall -s dftest.c -o test
test\$ ./test
34.000000``` 10. You must be doing something else wrong.

When you post code exhibiting a problem, trying copying the actual offending code, not writing code that does what you think the offending code does. By not doing that, you appear to be posting code that does not exhibit your problem. 11. Originally Posted by JohnDeon But it doesn't seem to be printing anything. I have a printf() before and printf() after the if statement it prints the first one but not the second one.
printf "debugging" is so not the way to go about seeing what your program is really doing. What you really need to do is to use a debugger to step through the program and see exactly what happens.

Alternatively, if you use Microsoft Visual Studio then you can insert trace points to produce output containing variable values etc as the program runs. In fact you can insert and remove these at will while the program is running. Try doing that with printf "debugging"! Popular pages Recent additions 