Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i try simple test data type float in c, float can represent a range between 10^-37 to 10^ 37, but exemple follow show wrong value when runtime.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

int main(int argc, char **argv)
{ 
	float init_f=3445678352.123; 
	fprintf(stderr,"%f\n", init_f );
		 
	return EXIT_SUCCESS;
}

/////////////
out put dislay
3445678336.000000


Please give me explan why this is wrong in c language?
Thanks.

What I have tried:

/////////////
out put dislay
3445678336.000000
Posted
Updated 24-Apr-24 2:19am
v2
Comments
Richard MacCutchan 24-Apr-24 4:22am    
Unless you are using numbers for complex mathematics, statisitics etc., you should not use floating point. Because of their inherent lack of precision they are best avoiided.
PIEBALDconsult 24-Apr-24 8:43am    
3445678352.123 can be represented in scientific notation as
3.445678352123E9 (if I have that right)
__^^^^^^^
and single-precision values have only about seven places of precision -- you are trying to exceed that.

A float has only about 7 decimal digits of precision. So for example given
C
#include <stdio.h>

int main()
{
    float f1 = 3445678336.0;
    float f2 = 3445678337.0;
    float result = f2 - f1;

    printf("%f\n", result);
}
The result is not 1, as expected, but 0, since, as a float, the values of f1 and f2 are indistinct. For better precision, use a double, which will give you about 15 decimal digits of precision.

You should prefer double to float in most cases for any new development, only using a float if you need to interface with a library or a data source that is expecting/providing a float.
 
Share this answer
 
Comments
CPallini 24-Apr-24 2:03am    
5.
Member 16250016 24-Apr-24 3:21am    
thanks,
To add to what k5054 has said, computers don't actually know about floating point numbers, they work in whole bytes and a float in C is stored in a relatively small amount of memory: 4 bytes (or 32 bits) only. This is broken into the mantissa (the "number part") the exponent ( the "number of tens part"). When you add two floats, the mantissa is "adjusted" to align the two exponents and then they can be added.

This means that the number of decimal digits that can be stored is quite small: 23 bits only, which holds about 7 digits ("About" because the number is obviously converted to binary first and not all decimal numbers convert "nicely"). This is called the "precision" of a number: how many digits are accurately stored and it has nothing to do with the "size" of a number: 1.234567 is as precise as 1234.567 because they have identical mantissas.

Doubles use the same system , but have 64 bits (8 bytes) to play with so have much higher precision.

Have a look here if you want more info: Floating-point arithmetic - Wikipedia[^]
 
Share this answer
 
Comments
CPallini 24-Apr-24 2:03am    
5.
Member 16250016 24-Apr-24 3:21am    
thanks,
 
Share this answer
 
Comments
Pete O'Hanlon 24-Apr-24 3:08am    
In the same spirit of generosity of vote as you have shown to others, this deserves a 5.
CPallini 24-Apr-24 3:22am    
You are too good a man.
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900