![]() |
Rounding and Truncating problem in C
I tested it using the purchase price if 23.455, when doing it Iuse 0.005 to help with the rounding issue however it is not rounding right when I truncate it
For example The Delmar store has a tax rate of 7.25% which should show a tax of 1.7004875. Rounding that it would be 1.70, even to account for the rounding in C with 0.005 it should still truncate to 1.70 however it is showing 1.71. What am I doing wrong? Here is the program I have at the moment Code:
#include <stdio.h> |
Re: Rounding and Truncating problem in C
Computers can't store floating point numbers precisely; they convert them to binary and there are some losses. You know about these losses in decimal, for example what's 3/10 as a number? 0.3333... you cannot represent it in a finite number of digits.
So it is with computers. When you try to store 1.70, the computer converts it into binary which only approximates the true value. So the maths on floating point numbers doesn't work properly. 1.70+0.01 != 1.71. There are two ways of getting round this. The most accurate way is to switch to integers, and instead of storing 1.70, store 170 with a note somewhere that this is the true value multiplied by 100. Then the maths will be exact. Or when comparing, you can subtract, take the absolute value and compare the difference with a suitably small number. So if you're comparing 1.70+0.01 with 1.71, what you might do is subtract 1.70 from 1.71 and see if the difference is less than, say, 0.00005. |
Re: Rounding and Truncating problem in C
Some one mentioned doing it basically this way
Code:
int sale = ((taxPercent[0]/100) * price) * 100;When I do this is comes out right with the first store. The second store however, with the number I use (23.455) of a tax rate of 7.5 shows a tax of 1.759125; shows in on there as 1.75 when it should show 1.76. Did I do it right and it should show the 1.75 or is there a way to do it to get it to be 1.76? I hope this all makes sense and thanks in advance for any help you can provide. |
Re: Rounding and Truncating problem in C
Looks like you need to round off. 1.759xxx -> 1.76. You can round to a whole number by adding half then casting to an int (so 1.4->1.9->1, 1.7->2.3->2, so this rounds to the nearest integer), so to round to the nearest 1/100th you can do something similar.
|
Re: Rounding and Truncating problem in C
Well it is allot of coding but I think I have done it. Tell me what you think
Code:
/******************************************************************** |
Re: Rounding and Truncating problem in C
"allot" isn't a word, or if it is, it doesn't mean "a lot". Let's see what happens if we use the "a xyyy"="axxyyy" principle some more...
Your code isn't formatted well. This makes it difficult to follow - a reviewer would need to reformat it to be able to make any sense of it. This isn't "allot" of code, by the way, it's only "affew" lines. The lack of good formatting will make this program "allot" harder than it needs to be. Here are the first few lines formatted correctly: Code:
int main (void) //Main program functionCode:
do { // starts a do while loopCode:
int main (void) //Main program function. Well duh. Obviously it's the main function.Code:
if (0.0 > price)Code review stops there because that's as far as I reformatted the code, except for one final point. Try to write self-documenting code, then that way the need for comments is reduced. For example Code:
return 0; // return of int main()Code:
return EXIT_SUCCESS; |
Re: Rounding and Truncating problem in C
These are stuff are teacher is telling us not to do. For instance our teacher is telling us to always use:
Code:
return 0;as far as the following Code:
if (0.0 > price)Code:
I appreciate your comments and sorry about the allot instead of a lot. I am not the best at grammar. |
Re: Rounding and Truncating problem in C
I just talked to the teacher and when confronted he said, he never mentioned it because everyone is assumed to have windows which automatically returns 0
This has happened before the teacher posted the following program used from an example Code:
#include <stdio.h>Code:
system("cls"); |
| All times are GMT +5.5. The time now is 13:43. |