Consider the following methods, found in the byar gem:
##
# Calculate lower boundary for observed cases
def self.lower_bound(obs, z_value = Z_VALUE)
return 0 if obs == 0
obs * (1 - 1.quo(9 * obs) - z_value.quo(3 * Math.sqrt(obs))) ** 3
end
##
# Calculate upper boundary for observed cases
def self.upper_bound(obs, z_value = Z_VALUE)
obs = obs + 1
obs * (1 - 1.quo(9 * obs) + z_value.quo(3 * Math.sqrt(obs))) ** 3
end
I would like to port these methods to Javascript, but I am unsure what quo does.
quo is a method defined on the Numeric class (and redefined in the Float class), which calculates the quotient of the receiver with the given argument. In other words, x.quo(y) is roughly equivalent to x / y, but more precise.
The difference here comes in when x and y are Fixnums (ie. an integer value):
> (1 / 2)
=> 0
> (1 / 2).class
=> Fixnum
> 1.quo(2)
=> (1/2)
> 1.quo(2).class
=> Rational
> 1.quo(2.5)
=> 0.4
> 1.quo(2.5).class
=> Float
Basically, quo ensures that the result of the division is expressed accurately by returing a Rational or Float, depending on the receiver and argument.
In Javascript, there isn't a distinction between different types of numbers, and division returns a floating point number already if necessary, so the first method can be expressed as:
obs * Math.pow(1 - 1 / (9 * obs) - z_value / (3 * Math.sqrt(obs)), 3)
Related
'Hi, I'm trying to calculate how many months to achieve a million dollars with compounding interest and monthly investments. There are my tries.'
'This first code work, but I want to replace the 92 in the rage with a compare formula like fv >= 1000000.'
'When I place the range like here, it doesn't work.'
Try while-loop may help:
pv = 130000 # present value
i = 4000 # regular monthly investment
r = 0.1375 # annual interest rate
n = 0 # number of months
# for n in range(0, 92):
fv = 0
while fv < 1000000:
fv = pv * (1 + r / 12) ** n + i * (((1 + r / 12) ** n - 1) / (r / 12))
n += 1 #don't forget
print(fv)
print(n)
You need to manually increase the value of n
Just like This question, I need to a positive number that is as close to zero as possible, without it being zero, but in Go.
The math package has a constant for this and similar values, just use that: math.SmallestNonzeroFloat64:
const (
MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
Printing its value:
fmt.Println(math.SmallestNonzeroFloat64)
Outputs (try it on the Go Playground):
5e-324
(Note: it's greater than the constant due to rounding in the fmt package.)
I made a method that takes two numbers and returns a calculated value rounded to three decimals. I'm curious to know how I can have numbers such as 1.141 to be rounded but numbers like 5.0 turned into integers (5).
code:
def calculateHypotenuse(a,b)
if (a <= 0 || b <= 0)
return raise
end
c = Math.sqrt((a * a) + (b * b))
return c.round(3)
end
not sure there is a built in function for floats, but a hackish way could be something like this.
def conditional_truncation(x)
x.truncate == x ? x.truncate : x
end
conditional_truncation(1.141)
=> 1.141
conditional_truncation(5.0)
=> 5
I am making a text adventure game and have to randomise the stats of my hero's enemies.
Is there a way to generate a random whole number from within a percentage range?
Like this: BaseHealth ± 10%, where BaseHealth is a variable.
def randomize_value(value, percent)
bottom = (value * (1 - percent / 100.0)).to_i
up = (value * (1 + percent / 100.0)).to_i
(bottom..up).to_a.sample
end
health = randomize_value(BaseHealth, 10)
This is assuming that health is to be integer.
If BaseHealth is integer,
def take_random base, percent
d = (base * percent / 100.0).to_i
base - d + rand(d * 2)
end
take_random(BaseHealth, 10)
or following Stefan's suggestion,
def take_random base, percent
d = (base * percent / 100.0).to_i
rand(base - d..base + d)
end
take_random(BaseHealth, 10)
I understand what you mean now
You can do this:
BaseHealth = ( rand(BaseHealth * 0.2) + BaseHealth*0.9 ).to_i
This can be accomplished with some basic arithmetic:
TotalHealth = BaseHealth + (BaseHealth * (rand(21)-10)/100)
This will take the BaseHealth and multiply it by a random number 0..20 minus 10, converted to a percent.
Assume BaseHealth = 20:
If rand returns 17, you get 7/100 = .07 so TotalHealth = 21.41
If rand returns 7, you get -7/100 = -.07 so TotalHealth = 18.6
I'm getting numbers like
2.36363636363636
4.567563
1.234566465448465
10.5857447736
How would I get Ruby to round these numbers up (or down) to the nearest 0.05?
[2.36363636363636, 4.567563, 1.23456646544846, 10.5857447736].map do |x|
(x*20).round / 20.0
end
#=> [2.35, 4.55, 1.25, 10.6]
Check this link out, I think it's what you need.
Ruby rounding
class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end
def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end
def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end
end
In general the algorithm for “rounding to the nearest x” is:
round(x / precision)) * precision
Sometimes is better to multiply by 1 / precision because it is an integer (and thus it works a bit faster):
round(x * (1 / precision)) / (1 / precision)
In your case that would be:
round(x * (1 / 0.05)) / (1 / 0.05)
which would evaluate to:
round(x * 20) / 20;
I don’t know any Python, though, so the syntax might not be correct but I’m sure you can figure it out.
less precise, but this method is what most people are googling this page for
(5.65235534).round(2)
#=> 5.65
Here's a general function that rounds by any given step value:
place in lib:
lib/rounding.rb
class Numeric
# round a given number to the nearest step
def round_by(increment)
(self / increment).round * increment
end
end
and the spec:
require 'rounding'
describe 'nearest increment by 0.5' do
{0=>0.0,0.5=>0.5,0.60=>0.5,0.75=>1.0, 1.0=>1.0, 1.25=>1.5, 1.5=>1.5}.each_pair do |val, rounded_val|
it "#{val}.round_by(0.5) ==#{rounded_val}" do val.round_by(0.5).should == rounded_val end
end
end
and usage:
require 'rounding'
2.36363636363636.round_by(0.05)
hth.
It’s possible to round numbers with String class’s % method.
For example
"%.2f" % 5.555555555
would give "5.56" as result (a string).
Ruby 2 now has a round function:
# Ruby 2.3
(2.5).round
3
# Ruby 2.4
(2.5).round
2
There are also options in ruby 2.4 like: :even, :up and :down
e.g;
(4.5).round(half: :up)
5
To get a rounding result without decimals, use Float's .round
5.44.round
=> 5
5.54.round
=> 6
I know that the question is old, but I like to share my invention with the world to help others: this is a method for rounding float number with step, rounding decimal to closest given number; it's usefull for rounding product price for example:
def round_with_step(value, rounding)
decimals = rounding.to_i
rounded_value = value.round(decimals)
step_number = (rounding - rounding.to_i) * 10
if step_number != 0
step = step_number * 10**(0-decimals)
rounded_value = ((value / step).round * step)
end
return (decimals > 0 ? "%.2f" : "%g") % rounded_value
end
# For example, the value is 234.567
#
# | ROUNDING | RETURN | STEP
# | 1 | 234.60 | 0.1
# | -1 | 230 | 10
# | 1.5 | 234.50 | 5 * 0.1 = 0.5
# | -1.5 | 250 | 5 * 10 = 50
# | 1.3 | 234.60 | 3 * 0.1 = 0.3
# | -1.3 | 240 | 3 * 10 = 30