We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to add a double literal
Page Index Toggle Pages: 1
How to add a double literal (Read 619 times)
How to add a double literal
Jan 13th, 2009, 9:54pm
 
Hello,

to make an interactive plot of how light is scattered by dust I need to calculate spherical Bessel functions and for this I need a so called gamma function... which boils down to a LUT for factorials (n! = 1 * 2 * 3 * ... * n) up to 170! . Problem is I need to write something like:

static double factorial_list[171]={1.,1.,2.,6.,24.,120.,720.,5040.,40320.,
362880.,3.6288e6,3.99168e7,4.790016e8,6.2270208e9,
8.71782912e10,1.307674368e12,2.0922789888e13,

.
.
.

1.0333147966386145e40,3.7199332678990125e41,
.
.
.
};

(code from a C++ library developend by NIST), and I cannot get the literals into processing. It seems that there is an automatic addition of F to each of the numbers which is of course wrong for 1.0333147966386145e40 and every higher number (they go up to 7.257415615307999e306).

How can I write that? Alternatively is there any posibility to get a processing Version consistently using double as the basic floating point type?

Thanks in advance,

George
Re: How to add a double literal
Reply #1 - Jan 13th, 2009, 10:24pm
 
It is hard, doubles are rarely needed in graphics (unless using scales or such), so they are apparently banned from Processing...

There is a possible workaround, a bit clumsy, perhaps somebody else will have a better idea:
Code:
double m = 1e38;
double x1 = 103.33147966386145 * m;
double x2 = 3719.9332678990125 * m;
println(x1 + " " + x2);
Re: How to add a double literal
Reply #2 - Jan 14th, 2009, 9:24am
 
Hello PhilHo

I considered the route you are describing, there is one real and one perceived problem with it. The real one first: for the power of 306 I will need eight multiplications with 1e38. Since each introduces a slight rounding error I will end up with a bit a different number than the one I wanted to have:

Code:

double m = 1e38;
double x1 = 7.257415615307999e2 *m *m *m *m *m *m *m *m;  
println("7.257415615307999e306 " + x1);

Result:

7.257415615307999e306 7.257413915244855E306

The perceived one: I need to do this conversion for over 600 numbers which is at least cumbersome.

So are there any other suggestions out there?

Regards,

George

PS: If you want to do any sort of scientific visualisation then doubles are a must IMHO.
Re: How to add a double literal
Reply #3 - Jan 14th, 2009, 10:05am
 
How is this for an ugly hack Wink

double m = Double.valueOf("1e38").doubleValue();  
double x1 = Double.valueOf("7.257415615307999e2").doubleValue() *m *m *m *m *m *m *m *m;  
println("7.257415615307999e306 " + x1);
Re: How to add a double literal
Reply #4 - Jan 14th, 2009, 11:24am
 
Ah, good idea Stephen, but then you don't need the multiplication.
Code:
double x1 = Double.parseDouble("1.0333147966386145e40");
double x2 = Double.parseDouble("3.7199332678990125e41");
double x3 = Double.parseDouble("7.257415615307999e306");
println(x1 + " " + x2 + " " + x3);

gskillas, you can even write a one-letter function wrapping Double.parseDouble if you want to keep the code more readable.

Perhaps we could suggest another syntax sugar: if we append 'd' to a number, it is left as double, ie. the d is removed and no f is added. double x = 3.14d;
Not sure if it is easy/doable with Antlr.
Re: How to add a double literal
Reply #5 - Jan 14th, 2009, 11:56am
 
Hello together,

the syntax Double.parseDouble("1.0333147966386145e40"); is  a bit ugly but marvelous Wink Guess it will have to do for the time being.

Thanks alot

George

Re: How to add a double literal
Reply #6 - Jan 14th, 2009, 7:07pm
 
or put your double stuff in a separate .java file (instead of a .pde) where the preprocessor won't affect it
Page Index Toggle Pages: 1