P: n/a

I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:
1. Nonnegative ==set to $0.00
2. Out of range (more than 10 digits to represent) ==set to $0.00
3. More than 2 decimal places ==truncate to 2.
I have the first two cases down.
I am having trouble with the floor function. If the input is 8.2, the
cost should be interpreted as $8.20, but this is not the case.
Using the algorithm I have right now, the entry condition to process
the number is the following where c is a double representing the cost.
if ( (c*100) != floor(c*100) ) {
c = floor(c*100)/100;
}
Why won't this work? First of all, it enters this block of code for
8.2, which it should not. 820 should equal the floored version of
820, right? wrong. The floored version comes out as 819.
For anything with 3 or more decimal places represented, it functions
as expected. For any number of the form x.y where y is between 0 and
5, exclusive, the block above will run, and the value will be
unchanged. Why??? I also have a cout statement in this block
indicating that the number had too many decimal places, and that it
had been truncated to ___. It says that it was truncated to the
original value, of course.
Am I missing something here? Why do those numbers enter this block?
10.325 is processed to 10.32 as expected, and 8.5 generates no errors
at all, but 3.8 enters the loop and comes out unchanged, as does 8.2.
Anyone?
I have also tried casting down to int and back to double for the
comparison, to truncate the fractional part of the number with the
cast; this does not help.  
Share this Question
P: n/a

On 3月23日, 上午10時05分, "lilma...@gmail.com" <lilma....@gmail.comwrote:
I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:
1. Nonnegative ==set to $0.00
2. Out of range (more than 10 digits to represent) ==set to $0.00
3. More than 2 decimal places ==truncate to 2.
I have the first two cases down.
I am having trouble with the floor function. If the input is 8.2, the
cost should be interpreted as $8.20, but this is not the case.
Using the algorithm I have right now, the entry condition to process
the number is the following where c is a double representing the cost.
if ( (c*100) != floor(c*100) ) {
c = floor(c*100)/100;
}
Why won't this work? First of all, it enters this block of code for
8.2, which it should not. 820 should equal the floored version of
820, right? wrong. The floored version comes out as 819.
For anything with 3 or more decimal places represented, it functions
as expected. For any number of the form x.y where y is between 0 and
5, exclusive, the block above will run, and the value will be
unchanged. Why??? I also have a cout statement in this block
indicating that the number had too many decimal places, and that it
had been truncated to ___. It says that it was truncated to the
original value, of course.
Am I missing something here? Why do those numbers enter this block?
10.325 is processed to 10.32 as expected, and 8.5 generates no errors
at all, but 3.8 enters the loop and comes out unchanged, as does 8.2.
Anyone?
I have also tried casting down to int and back to double for the
comparison, to truncate the fractional part of the number with the
cast; this does not help.
You forget considering the effect of the roundoff error of the
floatingpoint numbers.  
P: n/a
 li******@gmail.com wrote:
I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:
1. Nonnegative ==set to $0.00
2. Out of range (more than 10 digits to represent) ==set to $0.00
3. More than 2 decimal places ==truncate to 2.
I have the first two cases down.
I am having trouble with the floor function. If the input is 8.2, the
cost should be interpreted as $8.20, but this is not the case.
Why not just multiply the values by 100 and assign to an int? Then you
can work in cents.

Ian Collins.  
P: n/a

On 3月23日, 上午10時05分, "lilma...@gmail.com" <lilma....@gmail.comwrote:
I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:
1. Nonnegative ==set to $0.00
2. Out of range (more than 10 digits to represent) ==set to $0.00
3. More than 2 decimal places ==truncate to 2.
I have the first two cases down.
I am having trouble with the floor function. If the input is 8.2, the
cost should be interpreted as $8.20, but this is not the case.
Using the algorithm I have right now, the entry condition to process
the number is the following where c is a double representing the cost.
if ( (c*100) != floor(c*100) ) {
c = floor(c*100)/100;
}
Why won't this work? First of all, it enters this block of code for
8.2, which it should not. 820 should equal the floored version of
820, right? wrong. The floored version comes out as 819.
For anything with 3 or more decimal places represented, it functions
as expected. For any number of the form x.y where y is between 0 and
5, exclusive, the block above will run, and the value will be
unchanged. Why??? I also have a cout statement in this block
indicating that the number had too many decimal places, and that it
had been truncated to ___. It says that it was truncated to the
original value, of course.
Am I missing something here? Why do those numbers enter this block?
10.325 is processed to 10.32 as expected, and 8.5 generates no errors
at all, but 3.8 enters the loop and comes out unchanged, as does 8.2.
Anyone?
I have also tried casting down to int and back to double for the
comparison, to truncate the fractional part of the number with the
cast; this does not help.
To resolve the roundoff problem, you may add a tiny amount, e.g.
1.e10, to your number.
e.g.
Try this :
int main() {
const double TOL = 1.e10 ;
double a ;
cout << setprecision(2) << fixed ;
for ( a = 0 ; a < 20 ; a += 0.1 ) {
cout << a << "\t"
<< floor((a+TOL)*100)/100. << endl ;
}
return 0 ;
}
If you set the TOL to zero, then you'll see the roundoff problem
again.
BTW, The amount of TOL is problemdependent.  
P: n/a

On Mar 22, 10:13 pm, "weihan" <wei...@math.ncu.edu.twwrote:
On 323, 銝1005, "lilma...@gmail.com" <lilma...@gmail.comwrote:
I have a program in which I need to take a numeric value for dollars.
There is a "set" function that must screen the value for the following
3 conditions with the indicated handling functionality:
1. Nonnegative ==set to $0.00
2. Out of range (more than 10 digits to represent) ==set to $0.00
3. More than 2 decimal places ==truncate to 2.
I have the first two cases down.
I am having trouble with the floor function. If the input is 8.2, the
cost should be interpreted as $8.20, but this is not the case.
Using the algorithm I have right now, the entry condition to process
the number is the following where c is a double representing the cost.
if ( (c*100) != floor(c*100) ) {
c = floor(c*100)/100;
}
Why won't this work? First of all, it enters this block of code for
8.2, which it should not. 820 should equal the floored version of
820, right? wrong. The floored version comes out as 819.
For anything with 3 or more decimal places represented, it functions
as expected. For any number of the form x.y where y is between 0 and
5, exclusive, the block above will run, and the value will be
unchanged. Why??? I also have a cout statement in this block
indicating that the number had too many decimal places, and that it
had been truncated to ___. It says that it was truncated to the
original value, of course.
Am I missing something here? Why do those numbers enter this block?
10.325 is processed to 10.32 as expected, and 8.5 generates no errors
at all, but 3.8 enters the loop and comes out unchanged, as does 8.2.
Anyone?
I have also tried casting down to int and back to double for the
comparison, to truncate the fractional part of the number with the
cast; this does not help.
To resolve the roundoff problem, you may add a tiny amount, e.g.
1.e10, to your number.
e.g.
Try this :
int main() {
const double TOL = 1.e10 ;
double a ;
cout << setprecision(2) << fixed ;
for ( a = 0 ; a < 20 ; a += 0.1 ) {
cout << a << "\t"
<< floor((a+TOL)*100)/100. << endl ;
}
return 0 ;
}
If you set the TOL to zero, then you'll see the roundoff problem
again.
BTW, The amount of TOL is problemdependent.
thanks to both of you  it turned out that the project compiled and
ran without error, logic or otherwise, a couple days later. I don't
know if it was a temporary thing with the compiler or what, but it
works now. Thanks for the tips  will remember for next time!   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 4732
 replies: 4
 date asked: Mar 23 '07
