Page 1 of 1
Down scale
Posted: Sun Nov 30, 2014 12:52 am
by billv
eg:0-255 int scale down to 0-1 float.
Always had trouble with this sort of thing, but
recently came up with a solution that seems to work ok.
Just want to ask...is there a proper way to do this via math?
Re: Down scale
Posted: Sun Nov 30, 2014 1:28 am
by tulamide
As long as the result can be floats you can map any range to any other range.
source_min, source max, source_number, dest_min, dest_max
f = (dest_max - dest_min) / (source_max - source_min)
v = (source_number - source_min) * f + dest_min
Example
source: 0-255, dest: 0-1
f = (1 - 0) / (255 - 0) = 0.00392 (abbr.)
for each number 0-255 (example is 255 here)
v = (255 - 0) * f + 0 = 1
Re: Down scale
Posted: Sun Nov 30, 2014 1:32 am
by KG_is_back
yes... simply divide the int 0-255 input with 255 (make sure the math is done in float format). Or preferably pre-compute the value of 1/255 and multiply by that.
ruby code would look like this:
Code: Select all
value01=value255.to_f * 0.00392156862745098 #0.00392156862745098 is 1/255
To make sure you do the math in float format right click one of the input on divide prim (or multiply prim if you use that) and set the input to float... or connect the float number first.
Re: Down scale
Posted: Sun Nov 30, 2014 2:16 am
by billv
thanks guys....look forward to trying it when I get home..
Re: Down scale
Posted: Sun Nov 30, 2014 5:13 am
by billv
KG_is_back wrote: simply divide the int 0-255 input with 255
Wow..that was simple....
just for laughs...here's my dumb approach...
Create ruby version of green loop prim...set to 255 ticks with value 0.001
Feed into back of knob. set knob min/max 0-25, also create int output * 255
place "changed" prim on into output... on change push float value into a new array.
Hit ruby loop and get an array of 255 float values ranging from 0-1.
Re: Down scale
Posted: Sun Nov 30, 2014 5:21 am
by RJHollins
talk about 'two ways to 'skin-na-kat'
BTW ... I have some concoction of schematic designs that turned so evil ... I actually heard some form of growling every time I opened the schematic
Now I can't even figure out what I was trying to do ... scary ... just real scary ...
Re: Down scale
Posted: Sun Nov 30, 2014 6:15 am
by billv
RJHollins wrote:scary
Scary is seeing a solution and not been able to break it down to
"simply divide the int 0-255 input with 255"

Re: Down scale
Posted: Sun Nov 30, 2014 9:06 am
by tulamide
billv wrote:RJHollins wrote:scary
Scary is seeing a solution and not been able to break it down to
"simply divide the int 0-255 input with 255"

That's a bit unfair :'(
You said "eg:", so I answered with a universal valid formula that you can use for each task (like 42-51 -> -1-0), not just 0-255 -> 0-1. But if, and it seems so, this was all you're interested in, here's a Ruby version of your array approach:
Code: Select all
# my_array filled with values 0-255, for example Array.new(256) {|index| index}
my_array.map {|item| item.to_f / 255}
#my_array filled with values 0-1
Re: Down scale
Posted: Sun Nov 30, 2014 11:18 am
by Nubeat7
i normally use this methode:
Code: Select all
def scale_value(value,oldMin,oldMax,newMin=0.0,newMax=1.0)
scale = (newMax - newMin) / (oldMax - oldMin)
offset = newMin - oldMin * scale
return value * scale + offset
end
while Tulamides version is a bit smarter and needs 1 multiply less

depending on the need its pretty comfortable to use default values for the methode variables, the example above would fit well for billv's example and would look like this:
Code: Select all
def scale_value(value,oldMin,oldMax,newMin=0.0,newMax=1.0)
scale = (newMax - newMin) / (oldMax - oldMin)
return (value - oldMin) * scale + newMin #tulamide's version
end
def event i,v
output scale_value(@value,0,255)
end
this would scale from 0..255 to 0..1
if you need rescaling for arrays you find it also in my array class expansion here:
http://flowstone.guru/downloads/array-c ... expansion/
Re: Down scale
Posted: Sun Nov 30, 2014 11:41 am
by billv
@tulamide
Im still trying to understand your code., and KG's
believe it or not. So please take no offense.
The comment 'simply divide by 25', gave me a way
in , so I could actually see what I was doing with my
solution. Compared to my solution, it is a more
'proper' way..
Yes, your code shows a more effective way,
but I havnt been able to modify it yet.
Edit..
Thanks nubeat7...just saw your post after I posted