Discussion:
Round Float to nearest of pre-determined set of INT's
(too old to reply)
Barry
2007-07-19 15:54:44 UTC
Permalink
Does anyone have a simple routine or idea, how to round off a float to
the nearest fixed pre-determined set of Integers, namely to the closest of

2,5,10,20,50,100,200,500,1000

Without compiling a whole bunch of "if" smaller than, bigger than for
each of those Integer's

Im thinking more in the lines of dividing the float, by 10, then take
the ABS value, and then multiply by 10 again etc....but one still needs
to determine with "if"'s which INT it is nearest to, and thus which
value to divide by, abs, and multiply by.

Maybe a series of do{} while(value < INT); in ascending order of INT's
might do it effectively?

Any ideas?

Thanks

Barry
Thomas Maeder [TeamB]
2007-07-19 16:02:28 UTC
Permalink
Post by Barry
Does anyone have a simple routine or idea, how to round off a float to
the nearest fixed pre-determined set of Integers, namely to the closest of
2,5,10,20,50,100,200,500,1000
Linear or binary search, the comparison against both neighboring
values.
Barry
2007-07-20 06:15:53 UTC
Permalink
Thanks

I ended up using a series of if / else if in ascending order....not that
pretty but works well enough.
Post by Thomas Maeder [TeamB]
Post by Barry
Does anyone have a simple routine or idea, how to round off a float to
the nearest fixed pre-determined set of Integers, namely to the closest of
2,5,10,20,50,100,200,500,1000
Linear or binary search, the comparison against both neighboring
values.
Thomas Maeder [TeamB]
2007-07-20 15:26:37 UTC
Permalink
Post by Thomas Maeder [TeamB]
Linear or binary search, the comparison against both neighboring
values.
<sigh>

search, *then* comparison ...
Barry
2007-07-23 08:48:27 UTC
Permalink
Thomas, I apologize for the sometimes simple questions, and can
sympathise with your "sighs".

Programming is not my first language, as it where. I am an engineer
(post grad level) and only spend a small fraction of time programming.
Most of my efforts in this area going into further developing and
maintaining a program I initially developed in BCB Ver 1, some 10 yrs
ago, involving some 50 odd classes and about 30 000 lines of code, and
hence I do not spend anywhere enough time programming to keep up with
the evolutionary times of IT. Many components I have used (like some
graphing) having become obsolete during this time.

But question me on my area of specialty, and I could be the one
"sighing"....everyone has their area of expertise.

I appreciate any help I can get....off these newsgroups....from those
wiser than me in their various fields of expertise.

Thanks

Barry
Post by Thomas Maeder [TeamB]
Post by Thomas Maeder [TeamB]
Linear or binary search, the comparison against both neighboring
values.
<sigh>
search, *then* comparison ...
Thomas Maeder [TeamB]
2007-07-23 16:04:12 UTC
Permalink
Post by Barry
Thomas, I apologize for the sometimes simple questions, and can
sympathise with your "sighs".
No apology required. That sigh was about *my* typo ("the" instead of
"then"), which I suspected might have made my post unintelligible; it
was not related to anything you wrote.
Post by Barry
Post by Thomas Maeder [TeamB]
Post by Thomas Maeder [TeamB]
Linear or binary search, the comparison against both neighboring
values.
<sigh>
search, *then* comparison ...
--
Thomas
Loading...