Losing decimals

Hi people!!

I’m getting mad trying to get some points with a lot of decimals…

Maybe this is a stupid question or I’m crazy… but I remember using lot of decimals, with float or double variable types (in java).

I’m using “big numbers” and when I want to get the last values of the decimal part, the value is changing… Better to show the code:

double[] iz = {458691.451154, 4462621.015578};
double a = 458691.451154;
float f = 458691.451154;

void setup(){
  println("x1000: " +(iz[0]*1000)+" Double: "+iz[0]);  
  println(iz[0],iz[1]);  
  println(" \n THIS IS a: "+a);
  println(" THIS IS f: "+f);
  println(" THIS IS transformed a:  "+((a-458690)*100));
  println(" THIS IS transformed2 a: "+((a*100)-(458690*100)));
}

And this is the result:

x1000: 4.586914375E8 Double: 458691.4375
458691.4375 4462621.0
 
 THIS IS a: 458691.4375
 THIS IS f: 458691.44
 THIS IS transformed a:  143.75
 THIS IS transformed2 a: 143.75

Don’t know how to get that decimals that I’m losing or why I’m losing them… And why my number finishes with …1.451154 and the printed one finishes with …1.4375

Thanks :purple_heart:

1 Like

Inside “.pde” files, Processing’s preprocessor suffixes any numerical literal containing a point . and/or an e w/ an f, if it doesn’t have 1 already. :abacus:

So the PDE would add an f to 458691.451154, thus becoming 458691.451154f. :crazy_face:

But b/c it’s now a float datatype value, the Java compiler would truncate any decimal digits that wouldn’t fit within 4 bytes (32-bit)! :coffee:

So in order to stop that, just add your own suffix to the literal. :bulb:
For datatype double, the suffix is d or D: 458691.451154d. :flushed:

4 Likes

@GoToLoop Mmmm… I tried it with the float but it gave me the same 458691.44 (because of the rounding) so I thought it wasn’t working :man_facepalming:.

:upside_down_face::upside_down_face::upside_down_face::upside_down_face:

Thanks!! It works well :smiley: