Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Answer: How do I round a number?

by Roy Johnson (Monsignor)
on Apr 27, 2005 at 17:56 UTC ( #452056=note: print w/replies, xml ) Need Help??

in reply to Re: How do I round a number?
in thread How do I round a number?

See also ikegami's scratchpad (look for "Rounding"). Make sure your solutions work under all the same conditions.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Answer: How do I round a number?
by 5mi11er (Deacon) on Apr 27, 2005 at 18:19 UTC
    Wow, that's quite a scratchpad, thanks.

    So, I hadn't thought much about negative numbers. Is it mathematically correct for the ceiling function to go to the next more negative number (to the left on a number line) or to truncate (ie int) a negative number (move toward the right on a number line)?

    I think the rounding functions for negative numbers are correct (don't need adjusting), you're simply moving toward the nearest whole number...


    Update: As further discussed below, the code did originally have issues, the code in the answer above has now been replaced by working code.

      ceil should always round toward the right on a number line, so your ceil is correct. In fact, it's the same thing I came up with in Re^2: Perl oddities. Note merlyn's response, though.

      Caution: Contents may have been coded under pressure.
        Yes, the plain ceil function is fine, but the XL_ceil, and ceil_qtrs functions are (nearly certainly, without actually testing) broken for negative numbers.

        I'll have to play a bit later and see if I can fix those functions in a similar way...

        Update: Indeed there were issues with that code. I have /msg'ed the QandAEditors, and asked them to replace the bad code, but until they do, I've placed the new code on my scratchpad.


        Update: Erroneous code has since been replaced with working code.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://452056]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-29 02:42 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.