Representing an extremely small number in java?

A

adam.balgach

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

Andreas Wollschlaeger

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.

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
 
M

Mark Thornton

Andreas said:
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'!

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
 
E

EJP

Andreas said:
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.
 
M

mearvk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top