POV-Ray : Newsgroups : povray.programming : float literal quesion Server Time
22 Dec 2024 21:36:26 EST (-0500)
  float literal quesion (Message 1 to 3 of 3)  
From: Wolfgang Wieser
Subject: float literal quesion
Date: 29 Nov 2003 08:47:05
Message: <3fc8a358@news.povray.org>
Is there any reason to use
  a/=2.0;
instead of
  a*=0.5;
in case a is a float or double variable?

After all, 0.5 has an exact representation as float (being 
1 * 2^-1). On current CPUs, the latter is faster by a factor 
of three, IIRC. 

Wolfgang


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: float literal quesion
Date: 29 Nov 2003 08:59:07
Message: <3fc8a62b@news.povray.org>
In article <3fc8a358@news.povray.org> , Wolfgang Wieser <wwi### [at] gmxde>  
wrote:

> Is there any reason to use
>   a/=2.0;
> instead of
>   a*=0.5;
> in case a is a float or double variable?
>
> After all, 0.5 has an exact representation as float (being
> 1 * 2^-1). On current CPUs, the latter is faster by a factor
> of three, IIRC.

Unless you depend on certain precision side effects, the multiplication will
be sufficient.  And it isn't faster by a specific factor.  The issues behind
the speed difference are much more complex.  On modern processors,
multiplications can commonly be fully pipelines, while divisions are an
iterative process that is not pipelined (because it would take too many
gates).

    Thorsten

____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde

Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: Wolfgang Wieser
Subject: Re: float literal quesion
Date: 29 Nov 2003 09:29:40
Message: <3fc8ad53@news.povray.org>
Thorsten Froehlich wrote:

> In article <3fc8a358@news.povray.org> , Wolfgang Wieser <wwi### [at] gmxde>
> wrote:
> 
>> Is there any reason to use
>>   a/=2.0;
>> instead of
>>   a*=0.5;
>> in case a is a float or double variable?
>>
>> After all, 0.5 has an exact representation as float (being
>> 1 * 2^-1). On current CPUs, the latter is faster by a factor
>> of three, IIRC.
> 
> Unless you depend on certain precision side effects, the multiplication
> will be sufficient. 
>
It would be nice if you could elaborate on this. 

I can imagine that
  a*=0.2;
results in a precision degeneration compared to
  a/=5.0;
(because 0.2 cannot be represented exactly a dual system). 

But since 2 and 0.5 can be represented exactly as a float/double, 
I cannot imagine any precision decrease when using 
  a*=0.5;
instead of
  a/=2;
(It is simply a decrease-exponent-by-one operation.)

> And it isn't faster by a specific factor.  The issues behind
> the speed difference are much more complex.  On modern processors,
> multiplications can commonly be fully pipelines, while divisions are an
> iterative process that is not pipelined (because it would take too many
> gates).
> 
The factor three or four was not a specific measurement but a rule of 
thumb for people doing numerics. It should be a good estimate for 
everyday life ;)

Wolfgang


Post a reply to this message

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