473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Numerical Algorithm: floor to round to the nearest hundredth

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. Non-negative ==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.

Mar 23 '07 #1
4 5242
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. Non-negative ==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 round-off error of the
floating-point numbers.

Mar 23 '07 #2
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. Non-negative ==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.
Mar 23 '07 #3
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. Non-negative ==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 round-off problem, you may add a tiny amount, e.g.
1.e-10, to your number.
e.g.

Try this :

int main() {

const double TOL = 1.e-10 ;
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 round-off problem
again.
BTW, The amount of TOL is problem-dependent.

Mar 23 '07 #4
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. Non-negative ==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 round-off problem, you may add a tiny amount, e.g.
1.e-10, to your number.
e.g.

Try this :

int main() {

const double TOL = 1.e-10 ;
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 round-off problem
again.
BTW, The amount of TOL is problem-dependent.
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!

Apr 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: tertius | last post by:
Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 ...
6
by: fdunne2 | last post by:
Is there a function in C that rounds off a float to an int? e.g. 8.968 -> 9 or 3.217 -> 3. Regards, F.
10
by: tmeister | last post by:
I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
1
by: Tukewl4u | last post by:
Can someone direct me in the right direction on this? I just don't understand what it's asking. An Application of method.floor is rounding a value to the nearest integer. The statement y =...
11
by: elsa | last post by:
hi i have a question.. An application of function floor ia rounding a value to the nearest integer. the statement y=floor(x+0.5); rounds the number x to the nearest integer and assigns the result...
1
by: mallikarjunbc | last post by:
i would like to know thne source code in C langauge for any schuling algorithm like round robin,priority based.
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
7
by: mingke | last post by:
I've learned that we can round up and down a float number by using Ceil and floor function. But, is this function still able to work if I the number I want to round up (or down) is zero? I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.