POV-Ray : Newsgroups : povray.general : Function: Making negative numbers positive Server Time
1 Jun 2024 14:25:48 EDT (-0400)
  Function: Making negative numbers positive (Message 11 to 20 of 33)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Todd Carnes
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 11:21:07
Message: <565882f3$1@news.povray.org>
On 2015-11-27 03:15, Stephen wrote:
> On 11/27/2015 10:52 AM, clipka wrote:
>> Am 27.11.2015 um 05:12 schrieb Alain:
>>
>>> You can also use the sellect() function:
>>> #declare Abs = sellect(A, -A, A);
>>
>> The keyword is actually "select".
>>
>
> There is an 'L of a difference. ;-)
>

LOL! :)


Post a reply to this message

From: Alain
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 14:23:26
Message: <5658adae@news.povray.org>
Le 15-11-27 05:52, clipka a écrit :
> Am 27.11.2015 um 05:12 schrieb Alain:
>
>> You can also use the sellect() function:
>> #declare Abs = sellect(A, -A, A);
>
> The keyword is actually "select".
>
Oups


Post a reply to this message

From: Alain
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 14:26:01
Message: <5658ae49$1@news.povray.org>
Le 15-11-27 11:20, Todd Carnes a écrit :
> On 2015-11-27 02:45, clipka wrote:
>> That's only true if the number in question is known to be negative.
>>
>
> I know, but that was what the OP asked for.
>
>> In that case, however, it is easier and more efficient to just use the
>> unary minus operator:
>>
>> #declare PositiveNumber = -NegativeNumber;
>
> That works too, but then it's really just masking the fact that you are
> doing what I said... i.e. multiplying by -1.
>
> Todd
>

It's always safer to assume that the value in question *may* be negative 
as well as positive.
You want the result to be *always* positive indepentently if it's 
original sign.



Alain


Post a reply to this message

From: clipka
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 14:28:58
Message: <5658aefa$1@news.povray.org>
Am 27.11.2015 um 17:20 schrieb Todd Carnes:
> On 2015-11-27 02:45, clipka wrote:
>> That's only true if the number in question is known to be negative.
>>
> 
> I know, but that was what the OP asked for.
> 
>> In that case, however, it is easier and more efficient to just use the
>> unary minus operator:
>>
>> #declare PositiveNumber = -NegativeNumber;
> 
> That works too, but then it's really just masking the fact that you are
> doing what I said... i.e. multiplying by -1.

No, actually it's just the other way round: Multiplication by -1 is a
negation in a complicated disguise.


Mathematically, the definition of the negative of a value x has nothing
to do with multiplication whatsoever; instead, it is defined as the
inverse of x regarding the addition, i.e. the value that, when added to
x, will result in the neutral element 0.

In other words, the expression

    -x

is defined such that:

    x + (-x) = 0

See? No multiplication involved there. As a matter of fact, in its most
basic form the multiplication operation isn't even /defined/ for
negative numbers; defining whether the product of two negative values
should itself be negative or positive is actually a choice -- it doesn't
follow from first principles (although the choice that such a product
should be positive turns out to be helpful).


In computing, too, arithmetic negation is a well-established operation
in and of itself that has nothing to do with multiplication whatsoever.
CPUs had arithmetic negation operations long before multiplications were
implemented as dedicated machine code operations; earlier computers had
to implement multiplications as a series of additions, but could negate
with ease. And as a matter of fact, in the early days multiplication
functions (and later machine code operations) were sometimes unable to
deal with negative numbers entirely, and to compute the product of two
numbers of arbitrary sign it would have been necessary to first
determine the operands' signs and figure out whether the result should
be negative, then negate any negative operands to get their absolute
value, multiply those, and then negate the result again if it was
supposed to be negative.

In floating-point arithmetics as used by POV-Ray, numbers are typically
stored in sign-and-magnitude format, i.e. there is a bit indicating the
sign of the number, while all the other bits combined indicate the
magnitude. Thus, arithmetic negation is as simple as flipping the sign
bit. Multiplying a number by -1, on the other hand, is as complicated as
multiplying the magnitudes of the operands, and setting the result's
sign bit to the XOR of the operands' sign bits. In this context,
multiplying by 1 is as complicated as multiplying by any other value,
unless the compiler knows in advance that the value is 1 and can
therefore optimize the operation.


In POV-Ray, something even more surprising happens; let's look at the
following statement:

    #declare PositiveNumber = -1 * NegativeNumber;

This is actually equivalent to:

    #declare UNDEFINED_IDENTIFIER = -CONST * FLOAT_IDENTIFIER;

where UNDEFINED_IDENTIFIER happens to be called "PositiveNumber" and is
(presumably) undefined, CONST happens to be 1.0, and FLOAT_IDENTIFIER
happens to be called "NegativeNumber" and (presumably) holds some
negative number.

As you may notice, this statement includes a negation _and_ a
multiplication. The reason is that POV-Ray does not know negative
constants - all it gives you is positive constants and a negation operator.

(The same is also true in the C and C++ programming languages; in their
case, there are even situations where this matters, and can mess up the
code of unsuspecting programmers.)

As POV-Ray is not an optimizing compiler (actually it's no compiler at
all), it resolves the multiplication operation by blindly invoking the
CPU's floating point multiplication machine code operation, even if one
of the operands happens to be 1 in magnitude.

Obviously, multiplying a number with a negated positive constant takes
longer to execute than just negating the number in question -- and it
also takes longer to parse, requiring at least two more tokens (the
numeric literal "1" and the multiplication operator "*").


Post a reply to this message

From: clipka
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 14:37:49
Message: <5658b10d$1@news.povray.org>
Am 27.11.2015 um 20:26 schrieb Alain:
> Le 15-11-27 11:20, Todd Carnes a écrit :
>> On 2015-11-27 02:45, clipka wrote:
>>> That's only true if the number in question is known to be negative.
>>>
>>
>> I know, but that was what the OP asked for.
>>
>>> In that case, however, it is easier and more efficient to just use the
>>> unary minus operator:
>>>
>>> #declare PositiveNumber = -NegativeNumber;
>>
>> That works too, but then it's really just masking the fact that you are
>> doing what I said... i.e. multiplying by -1.
>>
>> Todd
>>
> 
> It's always safer to assume that the value in question *may* be negative
> as well as positive.
> You want the result to be *always* positive indepentently if it's
> original sign.

Actually we can't tell whether that's what the OP wants, as he never
told us the intended result for this case. (Though I'd agree it's a good
guess that he would have known about the negation or multiplication by
-1 if that had been the behaviour he was after.)

We can be pretty sure what /Todd/ wants though, and it's certainly /not/
what you claim he wants ;)


Post a reply to this message

From: Stephen
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 16:12:16
Message: <5658c730$1@news.povray.org>
On 11/27/2015 7:24 PM, Alain wrote:
> Le 15-11-27 05:52, clipka a écrit :
>> Am 27.11.2015 um 05:12 schrieb Alain:
>>
>>> You can also use the sellect() function:
>>> #declare Abs = sellect(A, -A, A);
>>
>> The keyword is actually "select".
>>
> Oups

Or Oops, in English. :-P

PS I know that I have a cheek correcting anyone's spelling but I could 
not resist it.

-- 

Regards
     Stephen


Post a reply to this message

From: Todd Carnes
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 17:40:30
Message: <5658dbde$1@news.povray.org>
On 2015-11-27 11:26, Alain wrote:
>>
>>
>
> It's always safer to assume that the value in question *may* be negative
> as well as positive.
> You want the result to be *always* positive indepentently if it's
> original sign

That's a good point.

Todd


Post a reply to this message

From: Todd Carnes
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 17:44:45
Message: <5658dcdd$1@news.povray.org>
On 2015-11-27 11:37, clipka wrote:
> We can be pretty sure what/Todd/  wants though, and it's certainly/not/
> what you claim he wants;)

Just as I can be "pretty sure" that what you are implying I want is wrong.

Todd


Post a reply to this message

From: Todd Carnes
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 17:46:06
Message: <5658dd2e$1@news.povray.org>
On 2015-11-27 11:28, clipka wrote:
>> That works too, but then it's really just masking the fact that you are
>> >doing what I said... i.e. multiplying by -1.
> No, actually it's just the other way round: Multiplication by -1 is a
> negation in a complicated disguise.
>
>
> Mathematically, the definition of the negative of a value x has nothing
> to do with multiplication whatsoever; instead, it is defined as the
> inverse of x regarding the addition, i.e. the value that, when added to
> x, will result in the neutral element 0.
>
> In other words, the expression
>
>      -x
>
> is defined such that:
>
>      x + (-x) = 0
>
> See? No multiplication involved there. As a matter of fact, in its most
> basic form the multiplication operation isn't even/defined/  for
> negative numbers; defining whether the product of two negative values
> should itself be negative or positive is actually a choice -- it doesn't
> follow from first principles (although the choice that such a product
> should be positive turns out to be helpful).
>
>
> In computing, too, arithmetic negation is a well-established operation
> in and of itself that has nothing to do with multiplication whatsoever.
> CPUs had arithmetic negation operations long before multiplications were
> implemented as dedicated machine code operations; earlier computers had
> to implement multiplications as a series of additions, but could negate
> with ease. And as a matter of fact, in the early days multiplication
> functions (and later machine code operations) were sometimes unable to
> deal with negative numbers entirely, and to compute the product of two
> numbers of arbitrary sign it would have been necessary to first
> determine the operands' signs and figure out whether the result should
> be negative, then negate any negative operands to get their absolute
> value, multiply those, and then negate the result again if it was
> supposed to be negative.
>
> In floating-point arithmetics as used by POV-Ray, numbers are typically
> stored in sign-and-magnitude format, i.e. there is a bit indicating the
> sign of the number, while all the other bits combined indicate the
> magnitude. Thus, arithmetic negation is as simple as flipping the sign
> bit. Multiplying a number by -1, on the other hand, is as complicated as
> multiplying the magnitudes of the operands, and setting the result's
> sign bit to the XOR of the operands' sign bits. In this context,
> multiplying by 1 is as complicated as multiplying by any other value,
> unless the compiler knows in advance that the value is 1 and can
> therefore optimize the operation.
>
>
> In POV-Ray, something even more surprising happens; let's look at the
> following statement:
>
>      #declare PositiveNumber = -1 * NegativeNumber;
>
> This is actually equivalent to:
>
>      #declare UNDEFINED_IDENTIFIER = -CONST * FLOAT_IDENTIFIER;
>
> where UNDEFINED_IDENTIFIER happens to be called "PositiveNumber" and is
> (presumably) undefined, CONST happens to be 1.0, and FLOAT_IDENTIFIER
> happens to be called "NegativeNumber" and (presumably) holds some
> negative number.
>
> As you may notice, this statement includes a negation_and_  a
> multiplication. The reason is that POV-Ray does not know negative
> constants - all it gives you is positive constants and a negation operator.
>
> (The same is also true in the C and C++ programming languages; in their
> case, there are even situations where this matters, and can mess up the
> code of unsuspecting programmers.)
>
> As POV-Ray is not an optimizing compiler (actually it's no compiler at
> all), it resolves the multiplication operation by blindly invoking the
> CPU's floating point multiplication machine code operation, even if one
> of the operands happens to be 1 in magnitude.
>
> Obviously, multiplying a number with a negated positive constant takes
> longer to execute than just negating the number in question -- and it
> also takes longer to parse, requiring at least two more tokens (the
> numeric literal "1" and the multiplication operator "*").
>

Thank you for taking the time to explain all this. It was actually much 
more complicated than I had first thought.

Todd


Post a reply to this message

From: clipka
Subject: Re: Function: Making negative numbers positive
Date: 27 Nov 2015 18:21:51
Message: <5658e58f$1@news.povray.org>
Am 27.11.2015 um 23:44 schrieb Todd Carnes:
> On 2015-11-27 11:37, clipka wrote:
>> We can be pretty sure what/Todd/  wants though, and it's certainly/not/
>> what you claim he wants;)
> 
> Just as I can be "pretty sure" that what you are implying I want is wrong.

Gotme ;)


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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