NO

Author Topic: Mysterious Output to a Simple Program  (Read 3242 times)

boral

  • Guest
Mysterious Output to a Simple Program
« on: January 07, 2013, 09:29:50 AM »
I was doing a program which will simply calculate the aggregate and percentage marks of 5 subjects for a student. The full marks for each subject is 100 and the marks are input through the keyboard. I know that this program can be written in a 1000 ways. But I write in the following way and I got a mysterious output: The last digit of the 'percentage' output is erroneous. Why is this happening ? - Please point out the error.  :(

Program:

#include<stdio.h>

main()
{
  float m1,m2,m3,m4,m5;
   printf(" enter the marks");
   scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
   printf(" Aggregate marks = %f \n Percentage marks = %f \n ", m1+m2+m3+m4+m5,(m1+m2+m3+m4+m5)/5);
   
}

Input: 53 25 26 98 49

Output:

Aggregate marks = 251.000000
Percentage marks = 50.200001

See the last digit of percentage marks. It is erroneous.

Note: I am using Pelles C , Version = 7.00.355

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Mysterious Output to a Simple Program
« Reply #1 on: January 07, 2013, 09:50:50 AM »
It's not erroneous. It is due to rounding.
You will find also on this forum some discussions about floating point arithmetic aproximations.
To fix simply reduce digits using something like "%8.3f" in your printf that will limit result to 8 digits total with 3 decimal places (or use one of the rounding functions).
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

CommonTater

  • Guest
Re: Mysterious Output to a Simple Program
« Reply #2 on: January 07, 2013, 02:55:20 PM »
You may also want to give this a read...
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
 
As Frankie points out, the accuracy of floating point math is limited by the size allocated to the variables (4 bytes, 8 bytes, etc) the least significant bit is always going to be a guess.  If you experiment, you will discover that doubles are considerably less inaccurate than floats; 8 bytes vs 4.
 
This, btw, is why you should never use floating point math for financial transactions. Limiting the displayed digits to get the apparent correct answer does not correct the underlying value. Over time the "last digit error" can become significant, causing ever bigger errors. The smart way to do money is to convert to the most basic unit (e.g. $1.07 becomes 107 pennies, in countries that work in dollars) and use integer math on that base unit. 
 
(And yes, this is the exploit featured in the movie "Office Space".)
« Last Edit: January 07, 2013, 03:22:02 PM by CommonTater »

boral

  • Guest
Re: Mysterious Output to a Simple Program
« Reply #3 on: January 07, 2013, 03:23:03 PM »
Thanks for the replies. They are really helpful.  :)

CommonTater, the link is great. Thanks.
« Last Edit: January 07, 2013, 03:27:19 PM by boral »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Mysterious Output to a Simple Program
« Reply #4 on: January 07, 2013, 08:37:30 PM »
This, btw, is why you should never use floating point math for financial transactions. Limiting the displayed digits to get the apparent correct answer does not correct the underlying value. Over time the "last digit error" can become significant, causing ever bigger errors. The smart way to do money is to convert to the most basic unit (e.g. $1.07 becomes 107 pennies, in countries that work in dollars) and use integer math on that base unit.
Well, that still doesn't help and might get you in trouble with "the man" when you have to work with sale tax/VAT. Try to add for the example the 9.25% sales tax that we have here in LA to your 107 pennies and sell that a few hundred times. Someone "up there" will not be very happy with you when you report the sales taxes you owe... ;)

Ralf

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Mysterious Output to a Simple Program
« Reply #5 on: January 07, 2013, 10:20:55 PM »
Even George Soros or Bill Gates will not run into problems with floating point maths. In June 2011, the gross volume of OTC derivatives topped 707 Trillion US$, more than ten times the World's GDP. This is the biggest single figure you can find in financial mathematics. Using the standard FPU, you can express this sum with a precision of one hundredth of a cent.