Velocity Reviews > pow(2, 1/2) != pow(2, 0.5) problem

# pow(2, 1/2) != pow(2, 0.5) problem

Michel Rouzic
Guest
Posts: n/a

 06-15-2005
I obtain an unwanted behavior from the pow() function :

when performing pow(2, 0.5), i obtain 1.414214
when performing pow(2, 1/2), i obtain 1.000000
when performing a=0.5; pow(2, a), i obtain 1.414214
when performing a=1/2; pow(2, a), i obtain 1.000000

how come??? and how can i do a pow(x, y) so my y is the fraction of two
other variables? (cuz for now it acts as if that fraction of two
variables in y was truncated)

Richard Bos
Guest
Posts: n/a

 06-15-2005
"Michel Rouzic" <(E-Mail Removed)> wrote:

> I obtain an unwanted behavior from the pow() function :
>
> when performing pow(2, 0.5), i obtain 1.414214
> when performing pow(2, 1/2), i obtain 1.000000

Your problem is not with pow(); it is with integer maths. How much is
1/2, again? And 1.0/2?

Richard

Michel Rouzic
Guest
Posts: n/a

 06-15-2005

Richard Bos wrote:
> "Michel Rouzic" <(E-Mail Removed)> wrote:
>
> > I obtain an unwanted behavior from the pow() function :
> >
> > when performing pow(2, 0.5), i obtain 1.414214
> > when performing pow(2, 1/2), i obtain 1.000000

>
> Your problem is not with pow(); it is with integer maths. How much is
> 1/2, again? And 1.0/2?
>
> Richard

so i gotta add .0 at the end of all my numbers in order to make it work?

Jean-Claude Arbaut
Guest
Posts: n/a

 06-15-2005

Le 15/06/2005 12:50, dans
(E-Mail Removed). com, «*Michel Rouzic*»
<(E-Mail Removed)> a écrit*:

>
>
> Richard Bos wrote:
>> "Michel Rouzic" <(E-Mail Removed)> wrote:
>>
>>> I obtain an unwanted behavior from the pow() function :
>>>
>>> when performing pow(2, 0.5), i obtain 1.414214
>>> when performing pow(2, 1/2), i obtain 1.000000

>>
>> Your problem is not with pow(); it is with integer maths. How much is
>> 1/2, again? And 1.0/2?
>>
>> Richard

>
> so i gotta add .0 at the end of all my numbers in order to make it work?
>

It's a good habit I would say... Otherwise one day you will forget, and
write 1/2 again, but in a place it will very difficult to find.

David Resnick
Guest
Posts: n/a

 06-15-2005

Michel Rouzic wrote:
> Richard Bos wrote:
> > "Michel Rouzic" <(E-Mail Removed)> wrote:
> >
> > > I obtain an unwanted behavior from the pow() function :
> > >
> > > when performing pow(2, 0.5), i obtain 1.414214
> > > when performing pow(2, 1/2), i obtain 1.000000

> >
> > Your problem is not with pow(); it is with integer maths. How much is
> > 1/2, again? And 1.0/2?
> >
> > Richard

>
> so i gotta add .0 at the end of all my numbers in order to make it work?

You "gotta" do no such thing. You just need to understand how
these things work. Arithmetic is based on the types of the operands.
If you divide an integer by an integer, you get an integer result.
If you divide an integer by a double, the integer is promoted to a
double
and the result is a double. Hence, you could do any of these:

1.0/2
(double)1/2
1/2.0
1/(double)2
1.0/2.0 /* Probably the clearest, but all work */

Don't do this though:
(double)(1/2)
The cast would be applied too late...

If any of the above is wrong, no doubt I'll be corrected

-David

Michel Rouzic
Guest
Posts: n/a

 06-15-2005

David Resnick wrote:
> Michel Rouzic wrote:
> > Richard Bos wrote:
> > > "Michel Rouzic" <(E-Mail Removed)> wrote:
> > >
> > > > I obtain an unwanted behavior from the pow() function :
> > > >
> > > > when performing pow(2, 0.5), i obtain 1.414214
> > > > when performing pow(2, 1/2), i obtain 1.000000
> > >
> > > Your problem is not with pow(); it is with integer maths. How much is
> > > 1/2, again? And 1.0/2?
> > >
> > > Richard

> >
> > so i gotta add .0 at the end of all my numbers in order to make it work?

>
> You "gotta" do no such thing. You just need to understand how
> these things work. Arithmetic is based on the types of the operands.
> If you divide an integer by an integer, you get an integer result.
> If you divide an integer by a double, the integer is promoted to a
> double
> and the result is a double. Hence, you could do any of these:
>
> 1.0/2
> (double)1/2
> 1/2.0
> 1/(double)2
> 1.0/2.0 /* Probably the clearest, but all work */
>
> Don't do this though:
> (double)(1/2)
> The cast would be applied too late...
>
> If any of the above is wrong, no doubt I'll be corrected
>
> -David

oh ok, i never know whether i get a float or int result, i mean, in my
case i had to do 1.0/(int variable).

how would i do if i wanted to divide two integers as if they were
floats without using some variable to transtype? just being curious

David Resnick
Guest
Posts: n/a

 06-15-2005
>Michel Rouzic wrote:
> > David Resnick wrote:
> > and the result is a double. Hence, you could do any of these:
> >
> > 1.0/2
> > (double)1/2
> > 1/2.0
> > 1/(double)2
> > 1.0/2.0 /* Probably the clearest, but all work */
> >
> > Don't do this though:
> > (double)(1/2)
> > The cast would be applied too late...
> >
> > If any of the above is wrong, no doubt I'll be corrected
> >
> > -David

>
> oh ok, i never know whether i get a float or int result, i mean, in my
> case i had to do 1.0/(int variable).
>

The result you get is based on the type. If you have constants,
things of the form "1" are integer constants, and things of the
form "1.0" are double constants. If you do an operation on two
integers, the result is an integer. If you do an operation on
a double and an integer, the result is a double. For other
types (and unsigned vs signed stuff), consult the standard
or a text to see how promotions work.

> how would i do if i wanted to divide two integers as if they were
> floats without using some variable to transtype? just being curious

I showed you 5 ways. What is your queston here? If you
mean a way to interprete 1/2 as division of two floats,
well, you can't. They are integers, and how the division
works is governed by the C standard. You need to cast or do
1.0/2 etc.

Do you mean how to divide a/b with a and b interpreted as doubles?
int a=1;
int b=2;
You just need to cast one of them, as in
double c = (double)a/b;

-David

pete
Guest
Posts: n/a

 06-15-2005
Michel Rouzic wrote:

> oh ok, i never know whether i get a float or int result, i mean, in my
> case i had to do 1.0/(int variable).

Actually, 1.0 is of type double.

> how would i do if i wanted to divide two integers as if they were
> floats without using some variable to transtype? just being curious

printf("%f\n", (double)1 / 2);

--
pete

akarl
Guest
Posts: n/a

 06-15-2005
Michel Rouzic wrote:
> I obtain an unwanted behavior from the pow() function :
>
> when performing pow(2, 0.5), i obtain 1.414214
> when performing pow(2, 1/2), i obtain 1.000000
> when performing a=0.5; pow(2, a), i obtain 1.414214
> when performing a=1/2; pow(2, a), i obtain 1.000000
>
> how come??? and how can i do a pow(x, y) so my y is the fraction of two
> other variables? (cuz for now it acts as if that fraction of two
> variables in y was truncated)

In C, `/' is sometimes used to denote division and sometimes not. If at
least one of the operands is a floating point number you will get the
expected result, but if both operands are integers you will get the
quotient of the division. This is one example of how C uses a familiar
symbol and makes it do something unexpected (the assignment operator is
another example). At least some languages got it right (e.g. Oberon) and
use for instance `:=' for assignment and `DIV' for the quotient of
division of integers.

Furthermore, when familiar symbols have a classical interpretation and a
"C" interpretation, how do we unambiguously express things like `x = y'
and `1/2' in source code comments?

-- August

pete
Guest
Posts: n/a

 06-15-2005
akarl wrote:
>
> Furthermore, when familiar symbols have a classical
> interpretation and a
> "C" interpretation,
> how do we unambiguously express things like `x = y'
> and `1/2' in source code comments?

/*
** You're allowed to use English in source code comments
** and you can say things like "one half"
*/

--
pete