Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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)

 
Reply With Quote
 
 
 
 
Richard Bos
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


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

Richard
 
Reply With Quote
 
 
 
 
Michel Rouzic
Guest
Posts: n/a
 
      06-15-2005


Richard Bos wrote:
> "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

>
> 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?

 
Reply With Quote
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-15-2005



Le 15/06/2005 12:50, dans
. com, «*Michel Rouzic*»
<> a écrit*:

>
>
> Richard Bos wrote:
>> "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

>>
>> 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.

 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      06-15-2005


Michel Rouzic wrote:
> Richard Bos wrote:
> > "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

> >
> > 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

 
Reply With Quote
 
Michel Rouzic
Guest
Posts: n/a
 
      06-15-2005


David Resnick wrote:
> Michel Rouzic wrote:
> > Richard Bos wrote:
> > > "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
> > >
> > > 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

 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57