473,573 Members | 2,514 Online

# Problem regarding sum of digits of floating point number in C

hi there,

recently i have got a problem regarding calculation of sum of digits in
a floating point or precision number. the weird behaviour of
compiler/language is preventing me from calculating the sum of all
digits. the compiler doesnt store the number as given by the user. some
of the precision digits are lost or changed (at will!). so by any
method, i m unable to reliably calculate the sum of provided number;
except i input the number as a string!

i have tried 2 types of logic -

first i use the intuitive logic of multiplying by 10 and adding the
integer part to the 'sum' in each iteration. all goes well until final
precision digit where this final digit is expanded. e.g. if 0.7 is
remaining, it is expanded to 0.69999999998! why dear, why r u doing
those things that we don want u to do?
/*************** *************** *************** ******/
width=strlen(lt oa((int)a,0,10) );
sprintf(str,"%* .*f",width,MAXL EN-width,a);
do{
if(*s=='.') // ignore '.'
continue;
if(!isdigit(*s) ) //we have got a char in num, so break away!
break;
else sum+=*s-0x30; //convert to digit from ASCII
}while(*++s);
/*************** *************** *************** ******/
in this case, the sprintf truncates or even expands(reverse of
rounding) long precision numbers.

even the good old gcc is truncating the precision digits at will.

this problem proved to be harder than what i thought initially.

any suggestions?

btw, sorry for this long post, but i m hurt u know? ;-)
TIA
Shreyas Kulkarni

Nov 14 '05 #1
1 3865
Shreyas Kulkarni wrote:
hi there,

recently i have got a problem regarding calculation of sum of digits in
a floating point or precision number. the weird behaviour of
compiler/language is preventing me from calculating the sum of all
digits. the compiler doesnt store the number as given by the user. some
of the precision digits are lost or changed (at will!). so by any
method, i m unable to reliably calculate the sum of provided number;
except i input the number as a string!

i have tried 2 types of logic -

first i use the intuitive logic of multiplying by 10 and adding the
integer part to the 'sum' in each iteration. all goes well until final
precision digit where this final digit is expanded. e.g. if 0.7 is
remaining, it is expanded to 0.69999999998! why dear, why r u doing
those things that we don want u to do?
/*************** *************** *************** ******/
width=strlen(lt oa((int)a,0,10) );
sprintf(str,"%* .*f",width,MAXL EN-width,a);
do{
if(*s=='.') // ignore '.'
continue;
if(!isdigit(*s) ) //we have got a char in num, so break away!
break;
else sum+=*s-0x30; //convert to digit from ASCII
}while(*++s);
/*************** *************** *************** ******/
in this case, the sprintf truncates or even expands(reverse of
rounding) long precision numbers.

even the good old gcc is truncating the precision digits at will.

this problem proved to be harder than what i thought initially.

any suggestions?

btw, sorry for this long post, but i m hurt u know? ;-)
TIA
Shreyas Kulkarni

You need a course in computer arithmetic!

Floats are generally represented using the IEEE standard #754.
http://en.wikipedia.org/wiki/IEEE_fl...point_standard
as 32 bits, 64 bits. Bog standard Intel x86 CPUs do all their floating point
arithmetic using the IEEE representation in hardware.

But this is base 2, and when you convert to/from base 10 rounding errors
occur and you get the errors you see.

Single precision (float in C) has about 6 to 7 significant digits; double
precision (double in C) has about 15 significant digits.

You can try
- using double instead of float
- using double, and rounding to say 10 significant digits.

gtoomey
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 2532 by: mg | last post by: Hi everybody... We try to white scripts with Pyrhon 2.4 for an acoustic simulation and we wrote these follow lines : c = 340 i =j=k= 1 sum_ = 23 table = 5 2681 by: towers | last post by: Hello, I've got a bit of experience in C++, but I'm writing my first app that is dependent on relatively precise math functions. The app requires that I get a time stamp based on s sample number, from a time series. This seems liek an easy thing to do: long lSample = 500; (for example) double dSampleRate = 1000.0; 21 12664 by: syntax | last post by: hi, i need to get high presion float numbers. say, i need pi = 22/7.0 = 3.142857....(upto 80 digits) is it possible ? does gcc/g++ compiler can give such type of high precision?? plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to 11 434 by: gouqizi.lvcha | last post by: Hi all I have a large vector with float point numbers in it, for example (1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to determine how many uique elements in the array? Thanks Rick 7 1964 by: Hiten | last post by: Hi please check ffollowing conditon variable float1 and float2 holds user entered value..... Answer=float1 * float2; //output must be 8.5 output must to be 8.5 but it has 8.500002, i am confused at this point i used calculators and other calculation mathods to see the actual value and thet give correct 8.5 with no other decimals...... 16 1873 by: Guenther Sohler | last post by: I have following code: int main(void) { printf("%.3lf\n",-2158470*0.001); } it prints -2158.470 14 3060 by: chun ping wang | last post by: Hey i have a stupid question. How do i get python to print the result in only three decimal place... Example>>> round (2.995423333545555, 3) 2.9950000000000001 but i want to get rid of all trailing 0's..how would i do that? _________________________________________________________________ 2 3469 by: Mukesh_Singh_Nick | last post by: What is meant by the "most significant digits" in the following statement?
With %g and %G, the precision modifier determines the maximum number of significant digits displayed.