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


pete wrote:
> 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


thank you, i had never heard of that (double) thing before what you
want to transtype before.

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


1/2 = 0
pow(2, 1/2) = pow(2, 0) = 1
pow (2, 1./2) = pow(2, 1/2.) = pow(2, 0.5)

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


1/2 = 0
see above.

> how come???


When you do arithmetic on integers, the arithmetic is done on integers.
What a surprize.

> and how can i do a pow(x, y) so my y is the fraction of two
> other variables?


This question is not related to the above.
in pow(2, 1/2) the power is the integer quotient of two integral
constants and has nothing to do with variables.

#include <math.h>
int main(void)
{
double a = 1, b = 2, x = 2, y, z;
y = a / b; /* "my y is the fraction of two other variables" */
z = pow(x, y);
return 0;
}
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-15-2005
Michel Rouzic wrote:
>
> pete wrote:
> > 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

>
> thank you, i had never heard of that (double) thing before what you
> want to transtype before.


It's a cast.
A cast converts the type and value of an expression to another.

--
pete
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      06-15-2005
In article <N8Wre.140319$(E-Mail Removed)>,
akarl <(E-Mail Removed)> wrote:
: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,

The Oberon Report indicates that / is "quotient" and DIV is
"integer quotient". You indicated that in C if both operands are
integers that you will get the "quotient" -- the same word used by
Oberon but with different meaning. What you wrote is thus inconsistant
with Oberon, so if Oberon "got it right" then either:
a) you "got it wrong" or;
b) you must admit that words and symbols are inherently ambiguous
and contextual.

http://www.oberon.ethz.ch/oreport.html#Expressions


If you are going to talk about "classical" interpretations
and "familiar symbols", then Oberon does *not* "get it right".
The "classical" meaning of / (solidus), dating back hundreds of
years, is as a seperator between shilling and pence in writing currency.
The use of solidus as meaning division only goes back a little over
a hundred years according to OED. The use of the solidus as
integer division in C (1972) is directly taken from the same use
in Kerninghan's B (1970) -- predating the decimalization of
UK coinage in 1971. Thus if you want to argue that C should have
adopted "classical" usages, then the use of the solidus should
indicate values in which the first portion is weighted 20 times the
second portion.

But you shouldn't even blame Kerninghan's "B" language. The use
of the solidus for integer division goes at least as far back
as the original FORTRAN specification in 1954. I refer you to
"D. FIXED POINT EXPRESSIONS" in
http://community.computerhistory.org...eport-1954.pdf

C's use of the solidus was thus "the familiar symbol" *to programmers*.
And if you read the history of C, you will note that Kerninghan and
Ritchie were not -intending- to write a language to be widely adopted
by the general public -- they weren't -intending- to write an
replacement for (say) Algol 68.
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
 
Reply With Quote
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-15-2005



Le 15/06/2005 20:35, dans http://www.velocityreviews.com/forums/(E-Mail Removed),
«*Lawrence Kirby*» <(E-Mail Removed)> a écrit*:

> For example some people (based on previous experience in the
> newsgroup) expect 1.0/10.0 to give a result of one tenth. On most
> implementations out there they would be wrong.


Could you explain ? If it's just the "0.1 is not representable"
problem, forget my question

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-15-2005
On Wed, 15 Jun 2005 13:35:41 +0000, akarl 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
>> 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.


When used as an operator in C / always denotes division.

> If at
> least one of the operands is a floating point number you will get the
> expected result,


That depends on what you expect. Expectations on this sort of thing do
vary. For example some people (based on previous experience in the
newsgroup) expect 1.0/10.0 to give a result of one tenth. On most
implementations out there they would be wrong.

> but if both operands are integers you will get the
> quotient of the division.


And for many people that is the expected result.

> This is one example of how C uses a familiar
> symbol and makes it do something unexpected (the assignment operator is
> another example).


As far as I know / is recognised in mathematics for operations on integer
domains, so C isn't doing anything abnormal here.

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


I agree that C would have been better off using := for assignment
and = for comparison, but it isn't a big deal. However / seems more
natural for division. Unless you also want create differrent operators for
integer addition, subtraction and multiplication.

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


If you mean x == y then write that, it is unambiguous. In general if there
is a C interpretation that is likely to be taken unless the context
indicates clearly otherwise.

Lawrence

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      06-15-2005
On Wed, 15 Jun 2005 20:23:25 +0200, in comp.lang.c , Jean-Claude
Arbaut <(E-Mail Removed)> wrote:

>
>
>
>Le 15/06/2005 20:35, dans (E-Mail Removed),
>«*Lawrence Kirby*» <(E-Mail Removed)> a écrit*:
>
>> For example some people (based on previous experience in the
>> newsgroup) expect 1.0/10.0 to give a result of one tenth. On most
>> implementations out there they would be wrong.

>
>Could you explain ? If it's just the "0.1 is not representable"
>problem, forget my question


Thats the answer.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      06-15-2005
Walter Roberson wrote:
> In article <N8Wre.140319$(E-Mail Removed)>,
> akarl <(E-Mail Removed)> wrote:
> :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,
>
> The Oberon Report indicates that / is "quotient" and DIV is
> "integer quotient". You indicated that in C if both operands are
> integers that you will get the "quotient" -- the same word used by
> Oberon but with different meaning. What you wrote is thus inconsistant
> with Oberon, so if Oberon "got it right" then either:
> a) you "got it wrong" or;
> b) you must admit that words and symbols are inherently ambiguous
> and contextual.


In the March 1995 edition of "The Programming Language Oberon-2",
section 8.2.2, `/' is called "real quotient". Hence there is no
inconsistency or ambiguity.
(http://control.ee.ethz.ch/edu/ciat1-...on2.Report.pdf)

According to page 6 in "Abstract Algebra" (second edition) by J. A.
Beachy & W. D. Blair:

<cite>
In familiar terms, the division algorithm states that dividing an
integer a by a positive integer b gives a quotient q and a non-negative
remainder r, such that r is less than b. You might write this as

a / b = q + r / b

</cite>

> If you are going to talk about "classical" interpretations
> and "familiar symbols", then Oberon does *not* "get it right".
> The "classical" meaning of / (solidus), dating back hundreds of
> years, is as a seperator between shilling and pence in writing currency.
> The use of solidus as meaning division only goes back a little over
> a hundred years according to OED. The use of the solidus as
> integer division in C (1972) is directly taken from the same use
> in Kerninghan's B (1970) -- predating the decimalization of
> UK coinage in 1971. Thus if you want to argue that C should have
> adopted "classical" usages, then the use of the solidus should
> indicate values in which the first portion is weighted 20 times the
> second portion.


Come on! `/' as a currency separator is neither widely known, nor of
current interest in a programming language.

> But you shouldn't even blame Kerninghan's "B" language. The use
> of the solidus for integer division goes at least as far back
> as the original FORTRAN specification in 1954. I refer you to
> "D. FIXED POINT EXPRESSIONS" in
> http://community.computerhistory.org...eport-1954.pdf
>
> C's use of the solidus was thus "the familiar symbol" *to programmers*.
> And if you read the history of C, you will note that Kerninghan and
> Ritchie were not -intending- to write a language to be widely adopted
> by the general public -- they weren't -intending- to write an
> replacement for (say) Algol 68.


Ok, this makes some sense.


-- August
 
Reply With Quote
 
James McIninch
Guest
Posts: n/a
 
      06-15-2005
<posted & mailed>

1 and 2 are integers.
So by integer division 1/2 = 0.
2 to the 0 power is 1.

C is correct.

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)


--
Remove '.nospam' from e-mail address to reply by e-mail
 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      06-15-2005
Lawrence Kirby wrote:
> When used as an operator in C / always denotes division.


No. Tell someone on the street that 1/2 equals 0.

>>If at
>>least one of the operands is a floating point number you will get the
>>expected result,

>
>
> That depends on what you expect. Expectations on this sort of thing do
> vary. For example some people (based on previous experience in the
> newsgroup) expect 1.0/10.0 to give a result of one tenth. On most
> implementations out there they would be wrong.


The inexact nature of floating point numbers is a different issue.

>>but if both operands are integers you will get the
>>quotient of the division.

>
>
> And for many people that is the expected result.


No, at least not according to the terminology used in "Abstract Algebra"
(see my previous post).

>>This is one example of how C uses a familiar
>>symbol and makes it do something unexpected (the assignment operator is
>>another example).

>
>
> As far as I know / is recognised in mathematics for operations on integer
> domains, so C isn't doing anything abnormal here.


Yes, but then it results in a quotient *and* a remainder. Note that it
was confusing enough to the original poster.

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

>
>
> I agree that C would have been better off using := for assignment
> and = for comparison, but it isn't a big deal. However / seems more
> natural for division.


It is, if it gives a floating point result. On the other hand, if for
instance 1/2 = 0 it's not division, it's something else.

> Unless you also want create differrent operators for
> integer addition, subtraction and multiplication.


Why would you want to do that?


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