Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Representing an extremely small number in java?

Thread view: 
adam.balgach@gmail.com - 07 Dec 2006 19:04 GMT
I have a requirement to represent a very very small number in a java
class of mine:

2.2204e-16

Does anyone have a straightforward solution to doing this? or am i
stuck with writing out something like

double _a = 0.0000000000000000....222204;

?

Thanks in advance,
A.
mearvk - 07 Dec 2006 19:10 GMT
Check out this API:
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html

I believe you can create arbitrarily small/precise decimals using an
implementation of this class.

mearvk
Andreas Wollschlaeger - 07 Dec 2006 19:31 GMT
> I have a requirement to represent a very very small number in a java
> class of mine:
[quoted text clipped - 10 lines]
> Thanks in advance,
> A.

How about

double a = 2.2204e-16d;

where the final 'd' makes sure it is a double literal; otherwise it
would be a 'real', implicitly casted to a 'double'!

Life can be that easy....

HTH
Andreas
Mark Thornton - 07 Dec 2006 20:13 GMT
>> I have a requirement to represent a very very small number in a java
>> class of mine:
[quoted text clipped - 17 lines]
> where the final 'd' makes sure it is a double literal; otherwise it
> would be a 'real', implicitly casted to a 'double'!

You don't need the 'd' suffix --- floating point values are double by
default. So this suffices

double a = 2.2204e-16;

By contrast you do need the 'f' suffix here:

float a = 2.2204e-16f;

Mark Thornton
EJP - 07 Dec 2006 23:35 GMT
> double a = 2.2204e-16d;
>
> where the final 'd' makes sure it is a double literal; otherwise it
> would be a 'real', implicitly casted to a 'double'!

What's a 'real'? The 'e' is enough to mark it as a double, you don't
need the 'd' as well.
mearvk - 08 Dec 2006 00:48 GMT
I should also add that since decimals are continuous (and the mapping
onto them is not), you only get a certain degree of accuracy using
Java's doubles. A double is 64 bits, which is quite alot. But remember,
there are an infinite number of decimals values between 0 and 1. So
when you do a comparison between two very small numbers which are
*similar*, the JVM may consider them to be equal, when in fact they are
not. This is where the BigDecimal implementation is a better solution.

May be germane, or not.

Mearvk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.