  | 
  | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
Has someone written a macro that rounds a float to the nearest integer? 
Thanks!
Mike
 
 Post a reply to this message 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
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 
 | 
  | 
 |   | 
 |   | 
 | 
  | 
 | 
  | 
 |   |