POV-Ray : Newsgroups : povray.general : Round to nearest integer Server Time
31 Oct 2024 12:14:49 EDT (-0400)
  Round to nearest integer (Message 1 to 8 of 8)  
From: Mike Horvath
Subject: Round to nearest integer
Date: 27 May 2021 07:20:56
Message: <60af8098$1@news.povray.org>
Has someone written a macro that rounds a float to the nearest integer? 
Thanks!


Mike


Post a reply to this message

From: Mike Horvath
Subject: Re: Round to nearest integer
Date: 27 May 2021 07:31:39
Message: <60af831b$1@news.povray.org>
On 5/27/2021 7:20 AM, Mike Horvath wrote:
> Has someone written a macro that rounds a float to the nearest integer? 
> Thanks!
> 
> 
> Mike


Or, to the nearest multiple of n, where n is any number.


Mike


Post a reply to this message

From: clipka
Subject: Re: Round to nearest integer
Date: 27 May 2021 09:14:53
Message: <60af9b4d$1@news.povray.org>
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.


Post a reply to this message

From: clipka
Subject: Re: Round to nearest integer
Date: 27 May 2021 09:17:26
Message: <60af9be6@news.povray.org>
Am 27.05.2021 um 13:31 schrieb Mike Horvath:
>> Has someone written a macro that rounds a float to the nearest 
>> integer? Thanks!
...
> Or, to the nearest multiple of n, where n is any number.

try `floor(FOO / N + 0.5) * N`.


Post a reply to this message

From: Mike Horvath
Subject: Re: Round to nearest integer
Date: 27 May 2021 09:32:07
Message: <60af9f57$1@news.povray.org>
On 5/27/2021 9:17 AM, clipka wrote:
> Am 27.05.2021 um 13:31 schrieb Mike Horvath:
>>> Has someone written a macro that rounds a float to the nearest 
>>> integer? Thanks!
> ...
>> Or, to the nearest multiple of n, where n is any number.
> 
> try `floor(FOO / N + 0.5) * N`.

I found this on web and adapted it to POV:

   #macro round(number, base)
     (((number + base/2) / base) * base)
   #end

Not sure which of the two is preferable.


Mike


Post a reply to this message

From: clipka
Subject: Re: Round to nearest integer
Date: 27 May 2021 13:42:49
Message: <60afda19$1@news.povray.org>
Am 27.05.2021 um 15:32 schrieb Mike Horvath:

>>> Or, to the nearest multiple of n, where n is any number.
>>
>> try `floor(FOO / N + 0.5) * N`.
> 
> I found this on web and adapted it to POV:
> 
>    #macro round(number, base)
>      (((number + base/2) / base) * base)
>    #end
> 
> Not sure which of the two is preferable.

You mean, aside from the fact that yours won't work, unless you put a 
`floor` between the first and second opening parentheses? ;)

Mine, I'd guess. It is comprised of only 10 tokens as opposed to 16 
(including the missing `floor`), and only 3 variable references as 
opposed to 4, so it will parse faster. The expression tree is also a bit 
simpler, so it will also execute faster, even if written as a function 
rather than a macro. (Remember, macros are parsed over and over again 
every time they are called. In general, functions are faster.)


Yours - especially with the lack of `floor` - looks like it was 
specifically designed for operating on purely integer values (i.e. not 
only is `number` expected to be an integer, but each operation 
automatically rounds down to an integer). In such a setting, it would be 
spot-on, to the point that you couldn't come up with a more efficient 
solution than that.

POV-Ray doesn't even know what an integer is.


Post a reply to this message

From: Alain Martel
Subject: Re: Round to nearest integer
Date: 27 May 2021 14:50:23
Message: <60afe9ef$1@news.povray.org>
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.


Post a reply to this message

From: clipka
Subject: Re: Round to nearest integer
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.