99.9% returns false in If Statement - google-sheets

I have an if statement and i cannot get a grip where I may have went wrong. everytime the results is 99.9% it is returning as "False", whereas it should within range and should result to "Awesome"
99.6 Good!
99.7 Great
99.8 Cool!
99.9 Super!
100 Prima!!
there is definitely something wrong with my statement.
=if(K2=100%,$M$5,if(K2>=99.9%,$M$4,if(K2>=99.8%,$M$3,if(K2>=99.7%,$M$2,if(K2>=99.6%,$M$1)))))
https://docs.google.com/spreadsheets/d/1TX49RPATclsWo7yUeJgUQ1x4CdBKAdniyxfX2DaWZLQ/edit?usp=sharing
Added a photo here

Expected output seems to be "Super!", because K2 is the first condition which matchs, 99.9 >= 99.9%, which is same as 99.9 >= 0.999.
Your bug is that %, it means /100. 99.9% is same as 0.999.
To fix: add % to the column with numbers, which is same as changing the numbers to be 0.996...1, or remove it from the formula.

Related

Lua float errors

I'm fairly new to lua, but I read that it does not have integers, so all numbers are floats.
I noticed in my log that some numbers get a slight inaccuracy added to it. For instance 0.14 is written as 0.14000000059605.
In that function it doesn't really matter if the number is a little diferent, as it is a comparison with a random number. But I do a lot of equals comparisons with numbes like NumReg() == 2 where it would give a wrong result if the 2 gets unrounded.
Do do I have to account for this by rounding them down, or are non-tiny numbers not affected?
You can format a float for your needs.
Example to play with in an interactive Lua console...
> _VERSION
Lua 5.4
tonumber(string.format('%.3f', 0.14000000059605))
0.14
> print(tonumber(string.format('%.3f', 0.14000000059605)))
0.14
> type(tonumber(string.format('%.3f', 0.14000000059605)))
number
> math.type(tonumber(string.format('%.3f', 0.14000000059605)))
float
> tonumber(string.format('%.3f', 0.14000000059605)) == 0.140
true

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)).

Erlang float_to_binary truncates decimals strangely

The Erlang float_to_binary function truncates decimals strangely. For instance, I would expect it to convert 0.45 with no decimal places to "0". Instead we get (example in Elixir):
iex> :erlang.float_to_binary(0.45, [decimals: 0])
"1"
iex> :erlang.float_to_binary(0.445, [decimals: 0])
"1"
> :erlang.float_to_binary(0.444, [decimals: 0])
"0"
Thus, it seems like rounding is being applied iteratively from right to left until the desired number of decimals is reached.
Is this expected behavior? Why doesn't it either round correctly or just truncate? Both of those options seem much more predictable to me.
This was a bug in Erlang which was fixed on Jan 15 2018 and first included in Erlang 20.3. If you upgrade to Erlang 20.3 or later, you should get "0" for 0.445.

Finding the number of digits in a number restricted number of tools since I am a Python beginner

def digits(n):
total=0
for i in range(0,n):
if n/(10**(i))<1 and n/(10**(i-1))=>1:
total+=i
else:
total+=0
return total
I want to find the number of digits in 13 so I do the below
print digits(13)
it gives me $\0$ for every number I input into the function.
there's nothing wrong with what I've written as far as I can see:
if a number has say 4 digits say 1234 then dividing by 10^4 will make it less than 1: 0.1234 and dividing by 10^3 will make it 1.234
and by 10^3 will make it 1.234>1. when i satisfies BOTH conditions you know you have the correct number of digits.
what's failing here? Please can you advise me on the specific method I've tried
and not a different one?
Remember for every n there can only be one i which satisfies that condition.
so when you add i to the total there will only be i added so total returning total will give you i
your loop makes no sense at all. It goes from 0 to exact number - not what you want.
It looks like python, so grab a solution that uses string:
def digits(n):
return len(str(int(n))) # make sure that it's integer, than conver to string and return number of characters == number of digits
EDIT:
If you REALLY want to use a loop to count number of digits, you can do this this way:
def digits(n):
i = 0
while (n > 1):
n = n / 10
++i
return i
EDIT2:
since you really want to make your solution work, here is your problem. Provided, that you call your function like digits(5), 5 is of type integer, so your division is integer-based. That means, that 6/100 = 0, not 0.06.
def digits(n):
for i in range(0,n):
if n/float(10**(i))<1 and n/float(10**(i-1))=>1:
return i # we don't need to check anything else, this is the solution
return null # we don't the answer. This should not happen, but still, nice to put it here. Throwing an exception would be even better
I fixed it. Thanks for your input though :)
def digits(n):
for i in range(0,n):
if n/(10**(i))<1 and n/(10**(i-1))>=1:
return i

Calculating a simple decimal in rails console using postgresql

Ok...I think I'm missing something very obvious here but haven't been able to google myself through this solution. I have two simple rails methods that calculate the number of up votes and down votes. They will always return a fraction because i'm trying to show a percentage (up_vote_count / votal_vote_count). I open the rails console and run the following:
y = #somespecificrecord
then...
y.up_vote_count
This returns 1 as is expected
y.down_vote_count
This returns 1 as is expected
y.total_vote_count
This returns 2 as is expected.
However, when I run in the console...
y.up_vote_count / y.total_vote_count
This returns 0 when it should return .50. I've been reading about floats/integers/decimals, etc and I do see this in the schema on the model i'm working from:
t.float "value", default: 0.0
Is this my problem?...and if so what do I have to do to allow myself to do a simple formula like the one above in rails console that will return the correct decimal rounded to 2 digits (i.e, .50 in this case above). I don't know if I want to run any migrations to change data types because this is a gem (& as a beginner I tend to stay away from customizing code from any gems I'm using). Is there another way? something small i'm missing hopefully?
UPDATE:
I'm learning decimals are slower than floats also, so is there any way to accomplish this with continuing to use t.float "value", default: 0.0
thanks for any help.
1 / 2 = 0.5
With integers this will round down to 0
You can get around this by casting the divisor to a float, forcing it to do division with floating point accuracy.
y.up_vote_count / y.total_vote_count.to_f
Float
Float objects represent inexact real numbers using the native
architecture's double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number.
its important to know if you divide 2 hole numbers you are going to get a hole number.
if you are looking a decimal you should first convert your numbers to a decimal or a "float" like this
up_votes = y.up_vote_count.to_f
total_vote = y.down_vote_count.to_f
(up_votes / total_vote) * 100.0
I hope that this helps

Resources