Re: rounding a number
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 27 Nov 2006 20:00:13 +0000 (UTC)
arman <previah@xxxxxxxxx> wrote:
How do you round some arbitrary number to the 10 multiplier? I need to
make a graph. The y-axis is dependent on the result of the computation.
If the maximum value of the y-axis, say for example 278, then I want
the y-axis to have a maximum of 300, or if it 2435 then I want the
maximum of 2500. So, basically it has to convert an arbitrary number
into some integer number that can be divided by 10.
You can start with rounding log10(), and then a loop to find
the appropriate multiple. Some allow multiplies of only 2 and 5,
others allow 2, 4, 5, 8, and possibly 3. (If plotting on
10 line per inch graph paper, 2 and 5 are best.)
Good axis label calculation isn't easy, especially if you allow
for graphs with the left or bottom coordinate non-zero.
Ones I knew did something like subtract the lower value from
the upper value, divide by the axis length, then take log10()
and round down. Next loop over the possible multipliers like
2, 4, 5, 8, 10, to see which one works.
Next, correct for the possibility that with the just determined
scale factor, the origin might not be on a grid line, requiring
recomputing the multiplier. Round the lower bound down and
the upper bound up to the appropriate multiple of as determined,
and test that the length is still right. If not, change the
multiplier and recompute the limits.
-- glen
.
- Follow-Ups:
- Re: rounding a number
- From: James Parsly
- Re: rounding a number
- References:
- rounding a number
- From: arman
- rounding a number
- Prev by Date: RE: rounding a number
- Next by Date: Re: random numbers in fortran
- Previous by thread: RE: rounding a number
- Next by thread: Re: rounding a number
- Index(es):
Relevant Pages
|