POV-Ray : Newsgroups : povray.newusers : rounding and logic functions Server Time
21 Nov 2026 10:29:16 EST (-0500)
  rounding and logic functions (Message 1 to 16 of 16)  
From: Bald Eagle
Subject: rounding and logic functions
Date: 2 Aug 2013 00:55:04
Message: <web.51fb3b6e744b4afe73fc9ebb0@news.povray.org>
I was plotting some data, (Version 3.6) and I had a lot of trouble getting
otherwise programmatically simple goals accomplished.

I see there are ceil() and floor() directives, though these seem suspect.

There's div(A,B)

There's mod(), the definition of which in the manual seems ... odd.
a remainder ought to be (A/B) - int(A/B)
But the manual says that this is them multiplied by B
Maybe I'm just ignorant abut the definition of "modulus", but then the textual
definition in the manual is misleading.

There's int()

Is there no simple ROUND() function?  To round a number to Q decimal places?


Also, how do I perform an OR comparison?

IF (X = A _OR_ B)?


Thanks again to those more knowledgeable than I.


Post a reply to this message

From: clipka
Subject: Re: rounding and logic functions
Date: 2 Aug 2013 01:52:36
Message: <51fb4924$1@news.povray.org>
Am 02.08.2013 06:54, schrieb Bald Eagle:
>
> I was plotting some data, (Version 3.6) and I had a lot of trouble getting
> otherwise programmatically simple goals accomplished.
>
> I see there are ceil() and floor() directives, though these seem suspect.

What's wrong with them?


> There's mod(), the definition of which in the manual seems ... odd.
> a remainder ought to be (A/B) - int(A/B)

That would be the fractional part of a division result, not the remainder.

Suppose you have ten pieces of cake, you have to split them fairly among 
three children, and cutting a piece into smaller ones is not an option. 
What's the result? Of course each child gets three pieces of cake, with 
one piece of cake /remaining/. Hence, an integer division of 10/3 yields 
3, and a /remainder/ of 1.

> But the manual says that this is them multiplied by B
> Maybe I'm just ignorant abut the definition of "modulus", but then the textual
> definition in the manual is misleading.

What's misleading about that? The formula given is unambiguous, the 
statement that mod(A,B) gives the value of A modulo B is straightforward 
(and true), and the statement that it returns the remainder after the 
integer division of A/B is also simple and true.


> Is there no simple ROUND() function?  To round a number to Q decimal places?

You can achieve a round-to-nearest-integer using ceil(X-0.5); You can 
achieve a round-to-Q-decimal-places by multiplying before and dividing 
after, such as in:

     ceil( X*pow(10,Q) - 0.5 ) * pow(0.1,Q)


> Also, how do I perform an OR comparison?
>
> IF (X = A _OR_ B)?

simple - just perform two individual comparisons and apply the OR 
operator ("|"):

#if ((x = a) | (x = b))


Post a reply to this message

From: clipka
Subject: Re: rounding and logic functions
Date: 2 Aug 2013 02:01:40
Message: <51fb4b44$1@news.povray.org>
Am 02.08.2013 07:52, schrieb clipka:

> the
> statement that mod(A,B) gives the value of A modulo B is straightforward
> (and true)

(...though strictly speaking it should state that mod(A,B) gives the 
smallest value that is "equivalent modulo B" to A, but nobody in the 
world of computer programming would express it this way)


Post a reply to this message

From: Bald Eagle
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 11:10:01
Message: <web.51fd1cfc87475f8873fc9ebb0@news.povray.org>
I was doing a simple #if - #else - #end loop and trying to trigger an update
once every "hour".
#if ( Time1 = ceil ( Time1 ) )

> What's wrong with them?

They just seemed to operate opposite to how I expected, but it could just be
that I was tired and the problem was my brain, not the functions.  I'm going to
investigate further.  With coffee.  :)


Your cake analogy cleared that up immensely.  I now can "see" the process
clearly in my mind.  Thanks!

> > Is there no simple ROUND() function?  To round a number to Q decimal places?
>
> You can achieve a round-to-nearest-integer using ceil(X-0.5); You can
> achieve a round-to-Q-decimal-places by multiplying before and dividing
> after, such as in:
>
>      ceil( X*pow(10,Q) - 0.5 ) * pow(0.1,Q)

Right.  I guess I wanted truly simple and straightforward, not the "you can
write your own formula for doing what should only t5ake invoking an easily
remembered and commonly used command".
It's also rather frustrating to a noob when searching for "round", and there's
no cross-index for floor and ceil, terms which no one would ever think to search
for.


> simple - just perform two individual comparisons and apply the OR
> operator ("|"):
>
> #if ((x = a) | (x = b))

Oh.  Bald Eagle, meet the OR operator.  OR operator, Bald Eagle.  I'll let you
two get acquainted.

That would have been a helpful cross index as well.
I searched for OR, Boolean, comparators, if, logic, functions, ....   no OR.


Post a reply to this message

From: clipka
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 17:20:16
Message: <51fd7410$1@news.povray.org>
Am 03.08.2013 17:08, schrieb Bald Eagle:

>> You can achieve a round-to-nearest-integer using ceil(X-0.5); You can
>> achieve a round-to-Q-decimal-places by multiplying before and dividing
>> after, such as in:
>>
>>       ceil( X*pow(10,Q) - 0.5 ) * pow(0.1,Q)
>
> Right.  I guess I wanted truly simple and straightforward, not the "you can
> write your own formula for doing what should only t5ake invoking an easily
> remembered and commonly used command".
> It's also rather frustrating to a noob when searching for "round", and there's
> no cross-index for floor and ceil, terms which no one would ever think to search
> for.

Well, that would be too easy, wouldn't it :-P

Frankly, I'm not the best person to discuss documentation matters; I'm 
happy when I can dump my description of new features into someone else's 
lap, and let them sort out where it fits best, where it should be 
cross-linked, and all that sort of stuff. James Holsenback has been 
doing great work in that respect, and he also has been giving the 
documentation a general facelift; I guess he'll be happy about hints 
like yours pointing out potential for even further improvement. After 
all there's one thing that becomes more and more difficult the more one 
gets acquainted to a piece of software and its documentation: To notice 
potential problems that new users might run into.


Post a reply to this message

From: Bald Eagle
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 17:55:01
Message: <web.51fd7bae87475f8873fc9ebb0@news.povray.org>
> Well, that would be too easy, wouldn't it :-P

Right-o.  What's life without a challenge, eh?

> I guess he'll be happy about hints
> like yours pointing out potential for even further improvement. After
> all there's one thing that becomes more and more difficult the more one
> gets acquainted to a piece of software and its documentation: To notice
> potential problems that new users might run into.

Indeed.  You acheive a certain myopia and over-familiarity.  That's why you have
OTHER people proofread your work, because you tend read right THROUGH the
errors.

I'm trying to illustrate to myself how things work, so maybe some of those ideas
will help out with clarifying the docs.


Post a reply to this message

From: Alain
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 18:35:39
Message: <51fd85bb$1@news.povray.org>
Le 13-08-03 11:08, Bald Eagle a écrit :
>
> I was doing a simple #if - #else - #end loop and trying to trigger an update
> once every "hour".
> #if ( Time1 = ceil ( Time1 ) )
>

Using such a loop would only work for a *very* long parse operation as 
it can only be evaluated during that time. When rendering or editing 
your scene, the loop is never used.


Alain


Post a reply to this message

From: James Holsenback
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 18:56:15
Message: <51fd8a8f$1@news.povray.org>
On 08/03/2013 05:52 PM, Bald Eagle wrote:
>
>> Well, that would be too easy, wouldn't it :-P
>
> Right-o.  What's life without a challenge, eh?
>
>> I guess he'll be happy about hints
>> like yours pointing out potential for even further improvement. After
>> all there's one thing that becomes more and more difficult the more one
>> gets acquainted to a piece of software and its documentation: To notice
>> potential problems that new users might run into.
>
> Indeed.  You acheive a certain myopia and over-familiarity.  That's why you have
> OTHER people proofread your work, because you tend read right THROUGH the
> errors.
>
> I'm trying to illustrate to myself how things work, so maybe some of those ideas
> will help out with clarifying the docs.
>
>

What did you have in mind ... I could add an index entry to ceil and 
floor so they come up when round is the search term. That would be on 
windows version only. Mileage may vary. As far as expanding the 
reference section 
http://wiki.povray.org/content/Reference:Numeric_Expressions#Functions 
(where ceil and floor are mentioned) I don't see a lot of room there 
given the format used for the rest of the functions. Maybe a small 
example /could/ be made to fit into the current scheme, but that 
/really/ doesn't do anything to address the fact you weren't aware that 
ceil and floor could be used like clipka showed ... at the moment kings 
x on coming up with a different layout. For the heck of it I took a 
quick tour of the tutorial and see that it's lacking in any sort of 
information about writing functions, so I guess I'm out of ideas


Post a reply to this message

From: Bald Eagle
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 21:45:01
Message: <web.51fdb16487475f8873fc9ebb0@news.povray.org>
Well, to start off, I find that things like this help me to understand things
WAY more than text.
http://news.povray.org/povray.binaries.tutorials/thread/%3Cweb.51f61e827603294573fc9ebb0%40news.povray.org%3E/
A picture's worth a thousand words, and all that.

Here's what my first impressions are when I look at that wiki page:

That green text is WAY too small.
I don't know what a unary minus or a unary plus is.
I'd expect mention of exponential functions in the precedence listing.
I'd also point out that (A^2) is not a valid expression - pow(x,2) is the way to
write that in POV-Ray.  And don't confuse pow() with pwr().
I think I tried doing an #if (X=<Y), but the <has to precede the =
I don't know what a bit-wise operation is
THEN should be searchable and linked to the ? operator

abs(A)
Absolute value of A. If A is negative, returns -A otherwise returns A.
#debug concat ("ABS -1 = ",str(abs(-1),3,0)) returns 1, not -1.

I don't know what hyperbolic trig functions are (off the top of my head)

#declare Step = 0;
#while (Step <= 2)
#debug concat ("Number = ",str(Step,3,1))
#debug "\n"
#debug concat ("ceiling = ",str(ceil(Step),3,1))
#debug "\n"
#debug concat ("floor = ",str(ceil(Step),3,1))
#debug "\n"
#debug "\n"
#declare Step = Step + 0.1;
#end

Floor and ceiling return the same value.
THAT's what's wrong with them.

For the function inside(), what is the "object identifier"? is that something
like #declare O=object{}?

strlen() is useful for text functions, but for rendering text in an image, I'd
like to know how large that text will actually be.

You have tan(A) and tanh(), but tan2(A,B) is ... elsewhere.

will val(s) accept val("3X")?

I HAD to render and animate the dot product file in order to understand what it
did.  The text didn't help me understand that it was a vector perpendicular to
the two vectors I was ... dotting.

.... and as a noob, I find it a little disorienting when an example uses some
obscure POV-Ray function without a prior simple/common one...
#if (image_width < 300) crand 0.1 #else crand 0.5 #end
.... because now I have to interrupt my train of thought and go off on some
tangent to look up what "crand" is.

So, there's an example of my constructive criticism.

Feel free to email me with requests for illustrations or proofreading /
commentary.

Thanks for what I'm sure is a TON of hard work, James.  I've been there, so I
know what it's like.


Post a reply to this message

From: Bald Eagle
Subject: Re: rounding and logic functions
Date: 3 Aug 2013 21:55:01
Message: <web.51fdb3a287475f8873fc9ebb0@news.povray.org>
Alain <kua### [at] videotronca> wrote:
> Le 13-08-03 11:08, Bald Eagle a écrit :
> >
> > I was doing a simple #if - #else - #end loop and trying to trigger an update
> > once every "hour".
> > #if ( Time1 = ceil ( Time1 ) )
> >
>
> Using such a loop would only work for a *very* long parse operation as
> it can only be evaluated during that time. When rendering or editing
> your scene, the loop is never used.
>
>
> Alain

Well, the parse and render phases are something I need to better understand,
because I long to do cool stuff with functions and macros, and I've seen that
asked plenty of times.  Lots of reading and experiments for me.

I used the loop in my render, and it seemed to work fine.

  #if ( Time1 = ceil ( Time1 ) )
   #declare A = A + B;
   #declare B = B + 2;
   text { ttf "arial.ttf" str (B, 3, 0) 0.1, 0 translate <Time1-2,
Level1*Y_Factor+2, -0.4> pigment {color Yellow}}
   #debug concat ("Time1 = ", str (Time1, 3, 1))
   #debug "\n"
   #debug concat ("A = ", str (A, 3, 1))
   #debug "\n"
  #else
  #end // end if

once I got the #if comparator right, it worked just like I expected it to.

#if ( Time1 / Step = int (Time1/Step)  DID NOT work.
Go figure.


Post a reply to this message

From: clipka
Subject: Re: rounding and logic functions
Date: 4 Aug 2013 09:32:00
Message: <51fe57d0@news.povray.org>
Am 04.08.2013 03:41, schrieb Bald Eagle:

> I don't know what hyperbolic trig functions are (off the top of my head)

Don't worry - nobody knows (and nobody really needs them) except for a 
few math geeks :-P

> #declare Step = 0;
> #while (Step <= 2)
> #debug concat ("Number = ",str(Step,3,1))
> #debug "\n"
> #debug concat ("ceiling = ",str(ceil(Step),3,1))
> #debug "\n"
> #debug concat ("floor = ",str(ceil(Step),3,1))
> #debug "\n"
> #debug "\n"
> #declare Step = Step + 0.1;
> #end
>
> Floor and ceiling return the same value.
> THAT's what's wrong with them.

Um... that might be because you wrote "ceil(Step)" in /both/ debug 
lines? :-P


Post a reply to this message

From: Alain
Subject: Re: rounding and logic functions
Date: 4 Aug 2013 21:44:29
Message: <51ff037d$1@news.povray.org>
Le 13-08-03 21:41, Bald Eagle a écrit :
>
> Well, to start off, I find that things like this help me to understand things
> WAY more than text.
>
http://news.povray.org/povray.binaries.tutorials/thread/%3Cweb.51f61e827603294573fc9ebb0%40news.povray.org%3E/
> A picture's worth a thousand words, and all that.
>
> Here's what my first impressions are when I look at that wiki page:
>
> That green text is WAY too small.
That depends on your browser settings for monospace text.
On most browsers, the default is effectively much to small.

> I don't know what a unary minus or a unary plus is.
An unary operator is the + or - that precede a single value. In "-A", 
the minus sign is an unary -.
In this expression:
-(A*-B)
The minus sign before the bracket and the one before "B" are unary minus 
signs.

> I'd expect mention of exponential functions in the precedence listing.
> I'd also point out that (A^2) is not a valid expression - pow(x,2) is the way to
> write that in POV-Ray.  And don't confuse pow() with pwr().
It's because the caret "^" operator can often become ambiguous.
What do you *realy* mean if you have 2^2/3?
It can be 2^(2/3) OR be (2^2)/3.
or what about 2^3^4...
It can be 2^(3^4) OR (2^3)^4.
In both cases, the result are NOT the same.
Depending on your background, or domain of work/activity, it can be 
evaluated either right to left or left to right.

> I think I tried doing an #if (X=<Y), but the <has to precede the =
It's also that way in both the BASIC, Fortran and C/C++ programing 
languages.

> I don't know what a bit-wise operation is
It's an operation that is conducten on each individual bit of some 
values. It's seldom used. It can only be used on integer values.

> THEN should be searchable and linked to the ? operator
In my view, it should rather be linked to the #if operator. It comes 
from the notation:
IF condition THEN action on true ELSE action on false

>
> abs(A)
> Absolute value of A. If A is negative, returns -A otherwise returns A.
> #debug concat ("ABS -1 = ",str(abs(-1),3,0)) returns 1, not -1.
And that's the correct result. abs(-1) = -(-1) = +1 or just 1.
It should have been told to you in elementary school. At least, I learnd 
that in 4th or 5th grade...

>
> I don't know what hyperbolic trig functions are (off the top of my head)
Nether do I.

>
> #declare Step = 0;
> #while (Step <= 2)
> #debug concat ("Number = ",str(Step,3,1))
> #debug "\n"
> #debug concat ("ceiling = ",str(ceil(Step),3,1))
> #debug "\n"
> #debug concat ("floor = ",str(ceil(Step),3,1))
> #debug "\n"
> #debug "\n"
> #declare Step = Step + 0.1;
> #end
>
> Floor and ceiling return the same value.
> THAT's what's wrong with them.
Just because you use str(ceil(Step),3,1)) for both strings.

>
> For the function inside(), what is the "object identifier"? is that something
> like #declare O=object{}?
Yes.
It can be any previously declared object.

>
> strlen() is useful for text functions, but for rendering text in an image, I'd
> like to know how large that text will actually be.
For that, you can use a monospace font where each character will be 1 
unit whide or use the min_extent() and max_extent() functions.

>
> You have tan(A) and tanh(), but tan2(A,B) is ... elsewhere.
>
> will val(s) accept val("3X")?
Yes and will return 3.

val("a35") will return zero.

It evaluate a string starting with some numeric character up to the 
first non-numeric character or the last one.
Numeric characters include the leter "e" as it's used for the 
exponential notation: 1e4 = 10000, 1.3e-2 = 0.012.

>
> I HAD to render and animate the dot product file in order to understand what it
> did.  The text didn't help me understand that it was a vector perpendicular to
> the two vectors I was ... dotting.
You are not alone. Many peoples don't understand that operation without 
that kind of example.

>
> .... and as a noob, I find it a little disorienting when an example uses some
> obscure POV-Ray function without a prior simple/common one...
> #if (image_width < 300) crand 0.1 #else crand 0.5 #end
> .... because now I have to interrupt my train of thought and go off on some
> tangent to look up what "crand" is.
>
> So, there's an example of my constructive criticism.
>
> Feel free to email me with requests for illustrations or proofreading /
> commentary.
>
> Thanks for what I'm sure is a TON of hard work, James.  I've been there, so I
> know what it's like.
>
>
>
>

Alain


Post a reply to this message

From: Alain
Subject: Re: rounding and logic functions
Date: 4 Aug 2013 22:01:43
Message: <51ff0787$1@news.povray.org>
Le 13-08-03 21:51, Bald Eagle a écrit :
> Alain <kua### [at] videotronca> wrote:
>> Le 13-08-03 11:08, Bald Eagle a écrit :
>>>
>>> I was doing a simple #if - #else - #end loop and trying to trigger an update
>>> once every "hour".
>>> #if ( Time1 = ceil ( Time1 ) )
>>>
>>
>> Using such a loop would only work for a *very* long parse operation as
>> it can only be evaluated during that time. When rendering or editing
>> your scene, the loop is never used.
>>
>>
>> Alain
>
> Well, the parse and render phases are something I need to better understand,
> because I long to do cool stuff with functions and macros, and I've seen that
> asked plenty of times.  Lots of reading and experiments for me.
>
> I used the loop in my render, and it seemed to work fine.
>
>    #if ( Time1 = ceil ( Time1 ) )
>     #declare A = A + B;
>     #declare B = B + 2;
>     text { ttf "arial.ttf" str (B, 3, 0) 0.1, 0 translate <Time1-2,
> Level1*Y_Factor+2, -0.4> pigment {color Yellow}}
>     #debug concat ("Time1 = ", str (Time1, 3, 1))
>     #debug "\n"
>     #debug concat ("A = ", str (A, 3, 1))
>     #debug "\n"
>    #else
>    #end // end if
>
> once I got the #if comparator right, it worked just like I expected it to.
>
> #if ( Time1 / Step = int (Time1/Step)  DID NOT work.
> Go figure.

It's probably due to some floating point rounding errors. If you have 
Time1=1000 and Step=10, then Time1/Step can result in something like 
99.9999999 that will show as 100 or 100.00001011 that also show as 100.
Now, int(99.999999) will evaluate as 99 whitch will fail the equality 
test with 99.999999.

That's because ALL calculations are performed as floating point and a 
division by 10 becomes a multiplication by 0.1, a value that can't be 
represented exactly using any floating point binary notation.
In fact, almost only values that are exact powers of 2 will give exact 
result in a division.
>
>

Your scene file is a text file.

The parse is the interpretation of the scene file. It goes over your 
code to find what to render.
It construct the actual data representing your scene in a totaly numeric 
form.
Once the parcing is termined, you can change and save your scene WITHOUT 
any effect on the resulting image. You can't save your modifications 
during the parse phase.

The rendering comes next, it's the step that actualy create the image 
that you can see from the numerical model created during the parse.



Alain


Post a reply to this message

From: Alain
Subject: Re: rounding and logic functions
Date: 4 Aug 2013 22:05:59
Message: <51ff0887$1@news.povray.org>
Le 13-08-04 21:44, Alain a écrit :

>> I'd expect mention of exponential functions in the precedence listing.
>> I'd also point out that (A^2) is not a valid expression - pow(x,2) is
>> the way to
>> write that in POV-Ray.  And don't confuse pow() with pwr().
> It's because the caret "^" operator can often become ambiguous.
> What do you *realy* mean if you have 2^2/3?
> It can be 2^(2/3) OR be (2^2)/3.
> or what about 2^3^4...
> It can be 2^(3^4) OR (2^3)^4.
> In both cases, the result are NOT the same.
> Depending on your background, or domain of work/activity, it can be
> evaluated either right to left or left to right.

Should have added some spaces...

What do you *realy* mean if you have 2^ 2/3?
It can be 2^(2/3) OR be (2^ 2)/3.
or what about 2^ 3^ 4...
It can be 2^(3^ 4) OR (2^ 3)^ 4.


Post a reply to this message

From: Bald Eagle
Subject: Re: rounding and logic functions
Date: 16 Aug 2013 23:10:01
Message: <web.520ee8b087475f8873fc9ebb0@news.povray.org>
> Should have added some spaces...
>
> What do you *realy* mean if you have 2^ 2/3?
> It can be 2^(2/3) OR be (2^ 2)/3.
> or what about 2^ 3^ 4...
> It can be 2^(3^ 4) OR (2^ 3)^ 4.

That's the purpose of heirarchy.

I'd write them the way you wrote them with the parentheses.
I've always learned that ^ precedes /, so 2^2/3 would be (2^2)/3.
But _allowing_ the use of a carat would be useful.

I suppose I could something clever with an include file...

[I can't believe I wrote that about abs... my brain must have been OFF!


Post a reply to this message

From: Alain
Subject: Re: rounding and logic functions
Date: 18 Aug 2013 17:00:26
Message: <521135ea$1@news.povray.org>
Le 13-08-16 23:06, Bald Eagle a écrit :
>
>> Should have added some spaces...
>>
>> What do you *realy* mean if you have 2^ 2/3?
>> It can be 2^(2/3) OR be (2^ 2)/3.
>> or what about 2^ 3^ 4...
>> It can be 2^(3^ 4) OR (2^ 3)^ 4.
>
> That's the purpose of heirarchy.
>
> I'd write them the way you wrote them with the parentheses.
> I've always learned that ^ precedes /, so 2^2/3 would be (2^2)/3.

Yes, but many peoples will use that for 2^ (2/3) by only adding a space 
between the ^ and the second 2, space that will be ignored by the parser.

> But _allowing_ the use of a carat would be useful.
>
> I suppose I could something clever with an include file...
>
> [I can't believe I wrote that about abs... my brain must have been OFF!
>
>
The caret WAS supported in older versions but was removed. I still find 
some old, legacy, scenes using the caret operator. They cause errors 
that are resolved using the pow(x, y) format.


Post a reply to this message

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