|
Author |
Topic: simple numeric functions (Read 276 times) |
|
battey
|
simple numeric functions
« on: Oct 4th, 2003, 8:35am » |
|
OK, maybe I've been programming in LISP too long and I'm missing something obvious... Why can't I do this, and is there a way TO do this in P5... that is, create a simple numeric utility function that returns a value. I've tried: float rescale(float value, float oldmin, float oldmax, float newmin, float newmax) { (((newmax - newmin) / (oldmax-oldmin)) * (value-oldmin) + newmin); } In this case p5 (a60) complains that 'float' is an unexpected token. Better yet, could I do this with the java 'number' type for inputs so it wouldn't insist on having floats as input?
|
|
|
|
toxii Guest
|
Re: simple numeric functions
« Reply #1 on: Oct 4th, 2003, 12:51pm » |
|
hi, i don't have any experience with LISP, but the obvious problem with your function is the lack of a "return" statement. apart from that Java is a strong-typed language so specifying the correct data type for each parameter and the return value of a function is essential. if you want to work with various data types (integers, doubles, your own ones...) you can create several version of a function with each taking a different number and/or type of parameters... Code:float rescale(float value, float oldmin, float oldmax, float newmin, float newmax) { return (((newmax - newmin) / (oldmax-oldmin)) * (value-oldmin) + newmin); } // "overloaded" version of rescale() using int's int rescale(int value, int oldmin, int oldmax, int newmin, int newmax) { return (((newmax - newmin) / (oldmax-oldmin)) * (value-oldmin) + newmin); } |
| also, you can cast most number types into a different format, sometimes obviously with loss of precision, eg. when converting float -> int, you can do that like this: int x=50; float newX=(float)( rescale(x,30,100,1000,2000) ); have a look at the reference for more info about those subjects.
|
|
|
|
battey
|
Re: simple numeric functions
« Reply #2 on: Oct 4th, 2003, 9:39pm » |
|
Ah, yes =) -- I now recall how frustrated and confused I was when I switched to LISP from C and LISP didn't have a "return" function. (LISP just returns the last form in the functionl). Obviously I got used to that eventually BUT... We have: float rescale(float value, float oldmin, float oldmax, float newmin, float newmax) { return (((newmax - newmin) / (oldmax-oldmin)) * (value-oldmin) + newmin); } Unfortunately, in both alpha 60 and 65 on Mac osX, float is still seen as an unexpected token. If you change the function to int rescale(float value... etc. "int" is an unexpected token. "void" isn't seen as an unexpected token -- after all, all of the online examples of functions are 'void' functions -- but of course that won't work for returning values... on alpha 59, the same code results in an error complaining that a ; or } is missing in the code. Does this post move to the bug list, or (hopefully) am I doing something syntactically awry?
|
|
|
|
fry
|
Re: simple numeric functions
« Reply #3 on: Oct 4th, 2003, 10:45pm » |
|
if you try to run that function, it's complaining because there isn't a body to the program itself. so it wasn't expecting to see a function definition, because it doesn't have a draw(), loop(), or setup() method. see the programming modes section of the reference: http://proce55ing.net/reference/environment/index.html since your program is just one function, then processing can't figure out which of the three modes you're in. it thinks you're using static mode, since there's no draw/setup/loop, but static mode can't have functions, so it gets unhappy. in lisp, of course, the function would be fine, and the program wouldn't do anything. this is just a side effect of the environment automatically detecting (or, uh, attempting to detect) what mode you're in.
|
« Last Edit: Oct 4th, 2003, 10:47pm by fry » |
|
|
|
|
|