normalizing number between 2 ranges - normalization

I know this may be a fairly basic thing but I'm having a lot of difficulty with it. I have some numbers which could be anywhere between 0.449999988079 and 10.0. What I would like to do is normalise these numbers so they are between 0.1 and 0.5. Could anyone help with achieving this please.
Thanks

function normalize(num, fromMin, fromMax, toMin, toMax)
{
return toMin + (num - fromMin)/(fromMax - fromMin) * (toMax - toMin)
}

Related

Modulo alternative Lua

I don't have much coding experience so I don't really know of an efficient alternative to modulo, the issue I have is that I want to have the same funcionality but witouth it ever returning zero if that makes sense.
So I have an arbritary value % 8 and I want my results to go (1,2,3,4,5,6,7,8,1,2,3,etc)
any help or push in the right direction would be appreciated.
I assume you're trying to make indices from 1 to 8 loop. For zero-based offsets from 0 to 7 this would be trivial by using i % 8; consider simply making your table zero-based.
For one-based indices, the simplest way to go is to first subtract 1 to make it zero-based, then apply the modulo to wrap around, then add 1 to make it one-based again: ((i - 1) % 8) + 1.
So I have an arbritary value % 8 and I want my results to go
(1,2,3,4,5,6,7,8,1,2,3,etc)
local result = value % 8 + 1
This is a simple maths problem. If one arrithmetic operator doesn't give you the desired result, use or add others to your formula.

If value of cell is positive do equation else do another equation

I'm trying to figure out how to pull a number from a cell and check if it's positive or negative and then from there do one of 2 equations to produce a final number. This is my code, but it doesn't work because the equations become strings. Not sure if there is a simple solution or if it requires a script of some sort since Google Sheets doesn't have an EVALUATE function.
=(CONCATENATE(IF(AC2>=0,"100 / (AC2 + 100) * 100","(AC2*-1)/ ((AC2*-1)+100) * 100)"),"%"))
Edit:
Final Code:
=CONCATENATE(IF(AC2>=0,ROUND(100/(AC2+100)*100, 0),ROUND((AC2*-1)/((AC2*-1)+100)*100, 0)),"%")
Remove the quotes around your equations so it looks more like..
=IF(AC2>=0,100/(AC2+100)*100,(AC2*-1)/((AC2*-1)+100)*100)
Any reason you are concatenating a %? Perhaps you just want to format it as a percentage.

LUA Rounding number to 1

I'm assuming there's a better way to do this other than writing a bunch of if statements. What I'm trying to do is round the number to the left down to 1. For instance, if a number is 12345.6789, round down to 100000.0000.. If the number is 9999999.9999, round down to 1000000.0000. Also want this to work with decimals, so if a number is 0.00456789, round it down to 0.00100000.
Any help or guidance would be greatly appreciated.
local function weird_rounding(num)
return 10 ^ math.floor(math.log(num, 10))
end

Lua math ceil round up number when there is no digits after decimal point

I would like to round up the numbers by using math.ceil in Lua.
Some of the cases are make sense like:
ceil(260.5) -> 261
But some of the cases are weird like:
ceil(2.2*100) -> 221 # Suppose there is no round up and the answer is 220
I have no idea why it acts like this, what should I do if I would like to round up the number when there are digits after decimal point, and no rounding up if there is just an integer?
-- Update:
Thanks for the answering from #cyclaminist:
2.2 * 100 is actually a little larger than 220.0 because 2.2 can't be represented exactly as a floating point number. Try ('%.15f'):format(2.2 * 100): for me, it gives '220.000000000000028'
Ceil will return the integer which is the closest and not smaller than 220.000000000000028, so that 221 is returned.
The solution to get 220 is:
math.floor(2.2*100 + 0.5) -> return 220, Since math.floor return the closest but not larger than 220.000000000000028
Try tonumber(string.format("%.0f",2.2*100)).

Actionscript rounding bug when dividing then multiplying

I am doing the following in actionscript in Coldfusion Flash Forms:
90 / 3.7
Gives me:
24.3243243243243
Whereas the calculator gives me:
24.32432432432432
Note the extra 2 at the end.
So my problem occurs when I am trying to get the original value of 90 by taking the 24.3243243243243 * 3.7 and then I get 89.9999999999 which is wrong.
Why is Actionscript truncating the value and how do I avoid this so I get the proper amount that the calculator gets?
Thanks so much.
Round your number using a routine like this
var toFixed:Function = function(number, factor) {
return (Math.round(number * factor)/factor);
}
Where the factor is 10, 100, 1000 etc, a simple way to think about it is the number of 0's in the factor is the number of decimal places
so
toFixed(1.23341230123, 100) = 1.23
Good explanation of numeric in ActionScript can be found at http://docstore.mik.ua/orelly/web2/action/ch04_03.htm. See section 4.3.2.1. Floating-point precision
A relavant quote:
"In order to accommodate for the minute discrepancy, you should round your numbers manually if the difference will adversely affect the behavior of your code. "

Resources