Floating point error in C


 
Thread Tools Search this Thread
Top Forums Programming Floating point error in C
# 1  
Old 12-21-2006
Floating point error in C

Hi,
see the simple code below

double i;
i=8080.9940;
printf(" val :%.30f\n",i);

output i m getting is
val :8080.993999999999700000000000000
when i m expecting
val :8080.9940

what happens?how can i avoid it?
thanks...
# 2  
Old 12-21-2006
Floating point numbers have a complex representation using base 2 rather than base 10. Scientific notation is close enough to understand what is happening. With scientific notation, we want a number between 1 and 10 multiplied by a power of 10: 8080.9940 = 8.0809940 * 10^3 and we can check that out with bc:
Code:
$ bc
bc 1.03 (Nov 2, 1994)
Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
8.0809940 * 10^3
8080.9940000
^D$

But now we need to do that in binary... We want a number between 1 and 10 in binary (which means 1 and 2 in decimal) that can be multiplied by a power of 10 in binary to yield the decimal number 8080.9940. Fortunately, you picked an easy number! It's is obvious that dividing it by 4096 will yield a number between 1 and 2. So let's use bc to do this...
Code:
$ bc -l
bc 1.03 (Nov 2, 1994)
Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=2
scale=40
8080.9940/4096
1.111110010000111111100111011011001000101101000011100101011000000100\
0001100010010011011101001011110001101010011111101111100111011011001
l(4096)/l(2)
1100.000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00
obase=10
ibase=2
1.1111100100001111111001110110110010001011010000 * 10^1100
8080.9939999999478459358215332031250000000000000000
1.11111001000011111110011101101100100 * 10^1100
8080.99399995803833007812500000000000000

Now repeat the last operation, but replace 1.11111001000011111110011101101100100 with some other close-by binary number. No matter what you try, you cannot get exactly 8080.9940 Floating point numbers are bit more complex than this, but the exact same issue arises with them. There is no way to exactly represent 8080.9940 as a floating point number.
# 3  
Old 06-18-2008
Hi,

You can try this code and will give the exact output which you are expecting

double i;
i=8080.9940;
printf(" val :%.4f\n",i);

Why because if you given %.30f it will consider 30 digit for fraction value so instead of %.30f we can use %.4f.

Let me know in case of any dificulties

Regards,
MPS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

floating point arithmetic operation error

I am writing a script in zsh shell, it fetchs a number from a file using the awk command, store it as a variable, which in my case is a small number 0.62000. I want to change this number by multiplying it by 1000 to become 620.0 using the command in the script var2=$((var1*1000)) trouble is... (2 Replies)
Discussion started by: piynik
2 Replies

2. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

3. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

4. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

5. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

6. UNIX for Dummies Questions & Answers

floating point error in linux + C

Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help! #include<stdio.h>... (1 Reply)
Discussion started by: headrush
1 Replies

7. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

8. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies

9. Shell Programming and Scripting

floating point addition

hi, :) I have a file like this 10.456 123.567 456.876 234.987 ........ ....... What i want to do is ia have to add all those numbers and put the result in some other file. Any help pls. cheers RRK (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

10. Shell Programming and Scripting

Floating Point Division

Does anyone have a simple way of doing floating point ("fp") division? For example, if I divide 3 by 5, I can get 0.6. The built-in calc (`bc`) will perform fp multiplication, but not division, at least not straight-up (i.e., starting bc and just typing in 3/5). I am trying to do this using... (1 Reply)
Discussion started by: gsatch
1 Replies
Login or Register to Ask a Question