FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   floats and doubles
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: floats and doubles  (Read 357 times)
fry


WWW
floats and doubles
« on: May 4th, 2004, 12:06am »

an emailed question from flight404:
 
I am working on a project that simulates a physics experiment from a
while back where someone suspended a dish of oil in a magnetic field
with the field being stronger at the edges and into it, the wacky
scientist dropped some magnetic particles of like charge into the dish
and found that they would organize themselves into a fibonacci spiral,
blah blah.
 
This brings me to my question.  I found that I was unable to use
doubles to run my experiment because the Math functions (some at least)
only take floats.  Would it be possible to create (for example) a SIN
function that checks for presence of float or double and executes the
correct one?  What would be the reason to not do this?  I was simply
hoping for a higher level of accuracy because after some time, the
experiments descends into chaos and I can only assume this is due to
the dropped decimals.
 
 
answer:
 
if you want to use doubles instead of floats, or to disable the automatic float -> double switch, use this:
compiler.substitute_floats = false
in pde.properties. this will make 0.4 a double and for a float you'll have to type 0.4f, just like java.
 
as for a version of the math functions, you can use java's Math.sin() and Math.cos(), which are based on doubles. the reason that sin() and cos() are included in the p5 api is because of the floats issue, where it's a way to avoid typing this sort of thing:
float s = (float) Math.sin(34.5f);
but since it's natively a double, the following will be no big deal:
double d = Math.sin(34.5);
i suppose typing Math. is not much fun, but hmm..
 
flight404

WWW Email
Re: floats and doubles
« Reply #1 on: May 4th, 2004, 12:14am »

Thanks for that concise answer.  I assumed there was something going on behind the scenes to explain this.  Good to know.
 
r
 
 
Quasimondo

WWW Email
Re: floats and doubles
« Reply #2 on: May 22nd, 2004, 1:57pm »

Could you elaborate what was the reason to use float instead of double as the default in Processing? As all the java Math functions use double as arguments and also return doubles I guess there must be two type conversions each time I use such a function. I guess that from a perfomance aspect this is probably worse than the overhead of the higher precision? But I'm not expert enough to be sure about that.
 
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
fry


WWW
Re: floats and doubles
« Reply #3 on: May 22nd, 2004, 3:42pm »

in short, you generally get much much better performance from floats than doubles.  
 
this is in part because doubles are 64 bits, and floats only 32, and 64 bit numbers require a bit more work by your cpu (multiple instructions) since it's most likely only a 32 bit machine.
 
in addition, for the type of work we're targeting with processing, doubles are complete overkill, so there's no need to take the performance hit. i've found in my own work, for instance, that it's extremely rare that i use doubles.
 
we also don't like teaching beginners that they have to put an 'f' after every number. it's goofy and adds a needlessly confusing aspect to the already foreign syntax.
 
all that said, it's easy to shut off, as i mentioned above. and java.lang.Math is there too so you're welcome to use that all you want.
 
Pages: 1 

« Previous topic | Next topic »