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)
   permanent decimal spot rounding?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: permanent decimal spot rounding?  (Read 730 times)
lunetta

7200475272004752 WWW Email
permanent decimal spot rounding?
« on: Mar 1st, 2005, 7:02pm »

Hello all
 
I'm wondering if there's a painless way to determine that all numbers everywhere in my sketch will have, lets say 5 decimal spots. So, all numbers above that should be rounded with a floor value...
example: 5.1234567890234 would become 5.12346...
 
 
What I have done is a function
 
float rounder (float number)
  {
   return floor(number*100000)/100000;
  }
 
but the problem is:
- it only works with floats
- it has to be called everywhere...  
 
This is not a problem per se, but a curiosity if I can define a number datatype just like if I were extending a built in class...
 
fry


WWW
Re: permanent decimal spot rounding?
« Reply #1 on: Mar 1st, 2005, 9:07pm »

you'd need to create some sort of a clever class and use that. the general method would be that you'd use an int (or a long) to store the numbers, and then pull em out as needed. there isn't really a way to extend the primitives (int, float, etc) that are allowed in processing.  
 
then again, if all you need is formatting for the user, nf() is a simple way to get decimals formatted cleanly into a string:
http://processing.org/reference/nf_.html
 
lunetta

7200475272004752 WWW Email
Re: permanent decimal spot rounding?
« Reply #2 on: Mar 1st, 2005, 10:14pm »

great, thanks for your response.
 
What I'm in need is to control the aberrations due to small numbers float imprecision, but I guess I'll keep using ints as much as possible and convert them to float only when necessary...
 
fry


WWW
Re: permanent decimal spot rounding?
« Reply #3 on: Mar 2nd, 2005, 3:52am »

you might also consider "fixed point" numbers, which are integer based, and might give you more straightforward results... a quick google or wikipedia search should explain how they work.
 
st33d

WWW Email
Re: permanent decimal spot rounding?
« Reply #4 on: Mar 2nd, 2005, 9:00pm »

I wondered about this myself and typed a load of words into google: float decimal point length java
 
http://www.prosperosoftware.co.uk/software/java/decimals/uk/co/prosperos oftware/pspnum/PSPvFloat.html
 
So if I've got this right, the floats only go up to six digits anyway. One less than six. To store a really fiddly number you would need a double, and then you need to trim it back to a float or an int to use it with the Processing functions (which don't seem to use doubles - thank God).
 

I could murder a pint.
lunetta

7200475272004752 WWW Email
Re: permanent decimal spot rounding?
« Reply #5 on: Mar 3rd, 2005, 6:10am »

wow, nice info, thanks
 
toxi

WWW
Re: permanent decimal spot rounding?
« Reply #6 on: Mar 3rd, 2005, 11:27am »

also posted a message (incl. examples) about fixed point numbers a (long) while ago over in this thread. it shouldn't be hard to wrap this in a class for use as custom datatype...
 

http://toxi.co.uk/
Pages: 1 

« Previous topic | Next topic »