0

In my homework, I have some problems with scanf function:

/* Can Doğu */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* My Function prototypes */
double calculate_profile_area(int edgecount,double edgelength); // Has two parameters: edgecount & edgelength. Returns double type value.
double calculate_volume(double a,double h); // Has two parameters: profilearea & boxheight. Returns double type value.
double calculate_price(double p); // Has one parameter: volume. Returns double type value.

/* My main function */
int main(void)
{

/* Variables & input-output part */
int count; double length,height,area,volume,price;
area=0.0,volume=0.0,price=0.0;
printf("Enter edge count for box profile: ");
scanf("%d",&count);
printf("Enter the profile-edge length and box-height: ");
scanf("%lf%lf",&length,&height);

/* Calling my functions */
area=calculate_profile_area(count,length);
volume=calculate_volume(area,height);
price=calculate_price(volume);

/* Printing the results */
printf("Box profile area is %.2lf cm square.\n",area);
printf("Box volume is %.2lf cm cube.\n",volume);
printf("Total sugar price is %.2lf TL.\n",price);
system("pause");
return(0);
}

/* calculate_profile_area function */
double calculate_profile_area(int edgecount,double edgelength)
{
double profilearea=0.0;
profilearea=edgecount*((edgelength*edgelength)/(4*tan(M_PI/edgecount)));
return(profilearea);
}

/* calculate_volume function */
double calculate_volume(double a,double h)
{
double v=0.0;
v=a*h;
return(v);
}

/* calculate_price function */
double calculate_price(double p)
{
double cost=0.0;
if(p>=0 && p<=500)
    cost=p*120;
else if(p>=501 && p<=1000)
    cost=p*100;
else if(p>=1001 && p<=2500)
    cost=p*80;
else if(p>=2501 && p<=5000)
    cost=p*65;
else if(p>=5001)
    cost=p*50;
return(cost);
}

When I enter a double value (2.15), my compiler reads it as 2.14999999. Thus my 'area' result is calculated as 54.59 (which should be 54.60). Any idea why my result is not correct?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Can Doğu
  • 1
  • 1
  • 1
    Please don't include pictures of your code, include the actual code in your question. – wpercy Dec 20 '15 at 16:14
  • 1
    Please paste source code into your question as plain text (add proper formatting, of course). See also: [Discourage screenshots of cod and/or errors](http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – zvone Dec 20 '15 at 16:18
  • It's a pity that the title and body of http://stackoverflow.com/questions/588004/is-floating-point-math-broken make it about computation errors, but the accepted answer in its current form encompasses errors in conversion to/from decimal. – Pascal Cuoq Dec 21 '15 at 02:00

1 Answers1

2

Short answer: floating-point numbers are frequently not represented exactly in computer systems. In general, computers use base 2, so a number such as 2.15, with a fractional part of 3/20, will not be exact.

More details on this issue can be found in “Why Are Floating Point Numbers Inaccurate?”. There is also Goldberg’s well-known paper on the subject, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”.

Community
  • 1
  • 1
Tom Zych
  • 13,329
  • 9
  • 36
  • 53