 |
Author |
Topic: int and float strangeness (Read 557 times) |
|
depth

|
int and float strangeness
« on: Nov 2nd, 2004, 11:31pm » |
|
been away from p5 for a while, and now it doesn't like me anymore. why does this: Code: return 0? so does this: Code: and this: Code: float newfloat = 1/6; println(newfloat); |
| this works okay: Code: println((float)1/(float)6); |
| but is rather ridiculous. ? thx -d
|
|
|
|
fjen
|
Re: int and float strangeness
« Reply #1 on: Nov 3rd, 2004, 12:29am » |
|
what processing returned is perfectly fine. try 1./6 or 1/6. ... either way, once one of the numbers is a float the result is a float. otherwise it's an integer as your tests show. /F
|
|
|
|
flight404

|
Re: int and float strangeness
« Reply #2 on: Nov 3rd, 2004, 1:13am » |
|
I generally get in the habit of adding .0 after any float that has no digits after the decimal place. I think it might be good coding practice. float x = 1.0; float y; y = x/2.0; and so forth.
|
|
|
|
fjen
|
Re: int and float strangeness
« Reply #3 on: Nov 3rd, 2004, 1:55am » |
|
yes, i agree ... i think a general what's-clean-code-tutorial would be good for beginners, considering some of the posts around here ... /F
|
|
|
|
gll

|
Re: int and float strangeness
« Reply #4 on: Nov 4th, 2004, 5:14am » |
|
Which one is better? (1.0/6.0) or (1f/6f)
|
guillaume LaBelle
|
|
|
flight404

|
Re: int and float strangeness
« Reply #5 on: Nov 4th, 2004, 6:34am » |
|
If I am not mistaken, the 'f' is being deprecated because a lot of people new to processing and java and the like get confused by its inclusion and usage. So, 1.0/6.0 would be preferred. Just try to remember that processing (java) deals with numbers in three primary ways. Integers - nothing after the decimal place So if you need have a counter of some sort, integers would be the way to go so you only need to deal with numbers like 1,2,40,50505, etc. Floats - have digits after the decimal They can range from 3.40282347E+38 to -3.40282347E+38 and are good to use when more accuracy is required from your calculations. A simple example is that with integers, PI would be represented as 3. This just isnt quite right. But using floats, PI would be 3.14159265 etc. Double - I have never really used this. I think Processing only supports them insomuch as it can be utilized through Java. I believe it is twice as accurate as floats, but tend to be hogs because who really needs ALL those digits past the decimal. I defer to the Java folk to clarify this. I think Java has all manner of numeric classifiers including integer, float, double, long, short, int, and byte. But for all the experiments I have made with Processing, I tend to use floats unless dealing with counters, loops, timers, etc. So, the short answer to your question, stick to 1.0/6.0 format. r UPDATE -------- Just saw that Java has numbers called AtomicInteger, AtomicLong, BigDecimal, and BigInteger as well. Chaos!!
|
« Last Edit: Nov 4th, 2004, 6:36am by flight404 » |
|
|
|
|
TomC
|
Re: int and float strangeness
« Reply #6 on: Nov 4th, 2004, 12:26pm » |
|
on Nov 4th, 2004, 5:14am, goo wrote:Which one is better (1.0/6.0) or (1f/6f) |
| Just to clarify Robert's answer. In Processing they are identical*, but the former is better. In Java, the former will be interpreted as doubles, the latter as floats. But a better coding style would probably be 1.0f/6.0f anyway. 1f and 1. don't look right to me (the former could be a muddled attempt at a hex integer, 0x1f which is 31, the latter is too subtle and easy to miss). To reiterate: in Processing, if you need accuracy, use a decimal point and a trailing zero. It will be compiled as a float. In Java, add the f suffix if you mean float, and leave it off if you mean double. It's also worth noting that 1.0/6.0 will almost certainly be precalculated by the compiler, so there's no need to worry about optimising the division, if anyone was wondering about that. *As far as I know, the preprocessor will add the f's on the end of 1.0 and 6.0 anyway.
|
« Last Edit: Nov 4th, 2004, 12:28pm by TomC » |
|
|
|
|
|