Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have doubt in the following code

Why it gives as answer as positive infinity.
Won't the -0 be promoted to -0.0. What is the reason
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arathi,
The answer is because you are using -0 instead of -0.. There is no such value -0 as an integer it is just 0 therefore the compiler performs the following:
-10. / -0
replaced with
-10. / 0
converted to
-10. / 0.
therefore the answer is Positive!

Only IEEE 754 recognizes a difference bewteen positive and negative zero!

If you had used the double negative zero you would get the result you expected without any replacement or conversion by the compiler.
Regards,
Manfred.
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order for the operation to take place, -0 must be cast to a double. To do that, the int value -0 must first be evaluated. Since there is no such int as -0, the value is evaluated as 0. This is cast to 0.0, and the operation is performed; leaving a result of POSITIVE_INFINITY.
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manfred Leonhardt:
Hi Arathi,
The answer is because you are using -0 instead of -0.. There is no such value -0 as an integer it is just 0 therefore the compiler performs the following:
-10. / -0
replaced with
-10. / 0
converted to
-10. / 0.
therefore the answer is Positive!

Only IEEE 754 recognizes a difference bewteen positive and negative zero!

If you had used the double negative zero you would get the result you expected without any replacement or conversion by the compiler.
Regards,
Manfred.

Thanks for your answer. I feel that when conversion by the compiler is made then 10.0/-0
is converted to
10.0/0. because -0 is converted as 0.0(with Casting). Am I correct. This what you meant to say. If its right, then i think you have to change the above code.
[ January 11, 2002: Message edited by: Arathi Rajashekar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic