Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exponential to float

 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good day,
how to convert from exponential(E) to ordinary float.

example : 1234.56E2 to 123456.00

have a nice day
krishna prasad
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krisp,
What data type is 1234.56E2? Java doesn't have a built in exponential type per se. It does store/display float/double numbers in that form if they are outside a certain threshold.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If 1234.56E2 is a String, you can convert to a float or double primitive with Float.parseFloat() or Double.parseDouble().

[Jeanne]: It does store/display float/double numbers in that form if they are outside a certain threshold.

The way the values are stored is exactly the same regardless of how big it is. Only the display format is different depending on the size.

Krisp, if you have

then x and y are in fact exactly the same, and both will be displayed the same way if you print them.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

then x and y are in fact exactly the same, and both will be displayed the same way if you print them.


Danger Will Robinson!
Floating point comparisons are not obvious. While Jim's example happens to be the same, just because the printed values of a float are the same, don't expect the values to be the same.

You are much safer using a fuzzy comparison:

if ((x-y) < 0.000001) then System.out.println("they are equal");
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pat]: While Jim's example happens to be the same,

Mmmm, they don't just happen to be the same - they're guaranteed to be the same. As long as both x and y are in the same JVM, anyway. Your more general point about avoiding using == for floating point is valid in most cases. But my point was that internally, both numbers are represented the same way regardless of whether exponential notation was used in the floating-point literal or not
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and the fuzzy comparison would be better as

or even



[ December 11, 2007: Message edited by: Jim Yingst ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[Jeanne]: It does store/display float/double numbers in that form if they are outside a certain threshold.

The way the values are stored is exactly the same regardless of how big it is. Only the display format is different depending on the size.

Yes. And I clearly should have made that two sentences so it would have been clearer that my if statement only referred to the display part. Java's easier. Doesn't let one get away with being imprecise
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But most definitely not if it looks like



[/CODE]
[ December 12, 2007: Message edited by: Pat Farrell ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most definitely not what? I'm not sure which part of Jeanne's statement you're responding to.

In general I'd write some utility methods:

Then most use cases can be handles cleanly and clearly with something like

Usually I'd prefer relative error. In some cases absolute error may be preferable, especially if both values are close to zero. But even then, relative error may be preferable. Depends on the situation.

Anyway, this has nothing to do with the original question. I don't know if Krisp still has questions about that or not.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my 'altered' code snippet is guaranteed to generate a divide by zero exception. Not what most folks expect in an equals() comparison.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pat Farrell:
my 'altered' code snippet is guaranteed to generate a divide by zero exception. Not what most folks expect in an equals() comparison.


No it's not, but it does produce an unexpected (to me anyway) result


The result I get is
x == y is true
they are not equal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic