POV-Ray : Newsgroups : povray.general : Round to nearest integer : Re: Round to nearest integer Server Time
25 Apr 2024 13:58:25 EDT (-0400)
  Re: Round to nearest integer  
From: clipka
Date: 27 May 2021 15:04:01
Message: <60afed21$1@news.povray.org>
Am 27.05.2021 um 20:50 schrieb Alain Martel:
> Le 2021-05-27 à 09:14, clipka a écrit :
>> Am 27.05.2021 um 13:20 schrieb Mike Horvath:
>>> Has someone written a macro that rounds a float to the nearest integer? 
>>
>> `floor(FOO + 0.5)` should do the trick.
> 
> «ceil(FOO - 0.5)» should also work.

In a technical sense, yes. However, values exactly halfway between would 
be rounded down, rather than up as per the common convention(*).

(For positive values, that is. For negative values, the common 
convention is to round such numbers towards negative infinity, making 
the `ceil`-based formula more true to conventional rounding in the 
negative domain.)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.