POV-Ray : Newsgroups : povray.general : Round to nearest integer : Re: Round to nearest integer Server Time
19 Apr 2024 10:27:15 EDT (-0400)
  Re: Round to nearest integer  
From: clipka
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

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