Split float into integer and decimals in Ruby - ruby

If I have a float like 3.75, how can I split it into the integer 3 and the float 0.75?
Do I have to convert the float into a string, and then split the string by ".", and then convert the parts into integers and floats again, or is there a more elegant or "right" way to do this?

You can use Numeric#divmod with argument 1 for this:
Returns an array containing the quotient and modulus obtained by dividing num by numeric.
a.divmod 1
=> [3, 0.75]
If you want to get precise value, this method is available for BigDecimal as well:
3.456.divmod 1
=> [3, 0.45599999999999996]
require 'bigdecimal'
div_mod = BigDecimal("3.456").divmod 1
[div_mod[0].to_i, div_mod[1].to_f]
=> [3, 0.456]

As mentioned you can use 3.75.to_i to get the integer.
An alternate way to get the remainder is using the % operator eg.
3.75 % 1

puts "frac = #{(3.456).to_s.split(".").last.prepend("0.").to_f}"
puts "Integer part = #{(3.456).truncate}"

Related

Float - two digits after comma - how to?

I tried to execute a calculation in Ruby. The result I get is 1589.5833333333333. I would like to limitat the numbers of digits after the comma.
The result should always be limited to 2 digits as followed:
1589.58
Question #1 = how can I set the limitation?
Question #2 = how can I round up 1589.60 or down 1589.55
Many thanks for help. Language is ruby
Other option but keeping the object as Float:
n = 1589.5833333333333
m = n.truncate(2) #=> 1589.58
h = n.round(1) #=> 1589.6 # for the last zero you need to format the string
And a tricky:
k = (n*100).to_i.digits.tap{ |ary| ary.first > 5 ? ary[0] = 5 : ary[0] = 0 }.reverse.join('').to_i/100.0
#=> 1589.55
For question 1:
num = 1589.5833333333333
printf('%.2f', num)
=> 1589.58
For question 2 to round up to first digit:
num = 1589.5833333333333
printf('%.2f', num.round(1))
=> 1589.60
1589.55 is a bit of an arbitrary number, rounding down would usually be calculated as 1589.58. I don't know of any Ruby function that does that off-hand.

How do I use multiple integers in an equation (Ruby)

I coded a conversion tool from binary to integer, but it had a limit on how large the number can be. So, I tried to code a formula for binary. I came up with an equation, so I tried to put it into code. Everything worked, except for applying the equation to each digit. This is the equation I came up with:
Let d represent the integer
Let z represent any (and every) digit
d = z[2^(z-1)]
This is what I've coded so far:
answer = gets.chomp
n = answer.reverse # reverses the answer
y1 = answer.size # the amount of digits in the answer
x1 = answer
z = (1..y1).each { |z| puts z } # every number between 1 and number of digits
w = (1..1).each.to_a * y1.to_i #in case I need to multiply the entire array
s = x1 # [z] - 1 # any given digit minus one
v = 2 ** s.to_i # exponent
u = z.zip(w).map{|x, y| x * y} # an array: [1, 2, 3]
print u
t = u.to_i # Tried converting to integer
puts x1[t]
But when I ran that, for example, with the number 1011, I got this error:
[1, 2, 3, 4]
undefined method `to_i' for [1, 2, 3, 4]:Array
Did you mean? to_s
to_a
to_h
(repl):16:in `<main>'
I feel like I have tried everything, but if you somehow find a way to apply the equation to every digit, or if you come up with a simpler equation, please tell me.
This return an array u = z.zip(w).map{|x, y| x * y} so you are triying to conver an array to integer. If you want, you can do something like this:
array = [1,0,1] #your binary in array form
s = array.join('') #transform it into string
s.to_i(2) #this return the integer and result (2) represents base
Check this link
And for better: array.join('').to_i(2)

Generating random number of length 6 with SecureRandom in Ruby

I tried SecureRandom.random_number(9**6) but it sometimes returns 5 and sometimes 6 numbers. I'd want it to be a length of 6 consistently. I would also prefer it in the format like SecureRandom.random_number(9**6) without using syntax like 6.times.map so that it's easier to be stubbed in my controller test.
You can do it with math:
(SecureRandom.random_number(9e5) + 1e5).to_i
Then verify:
100000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.map { |v| v.to_s.length }.uniq
# => [6]
This produces values in the range 100000..999999:
10000000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.minmax
# => [100000, 999999]
If you need this in a more concise format, just roll it into a method:
def six_digit_rand
(SecureRandom.random_number(9e5) + 1e5).to_i
end
To generate a random, 6-digit string:
# This generates a 6-digit string, where the
# minimum possible value is "000000", and the
# maximum possible value is "999999"
SecureRandom.random_number(10**6).to_s.rjust(6, '0')
Here's more detail of what's happening, shown by breaking the single line into multiple lines with explaining variables:
# Calculate the upper bound for the random number generator
# upper_bound = 1,000,000
upper_bound = 10**6
# n will be an integer with a minimum possible value of 0,
# and a maximum possible value of 999,999
n = SecureRandom.random_number(upper_bound)
# Convert the integer n to a string
# unpadded_str will be "0" if n == 0
# unpadded_str will be "999999" if n == 999999
unpadded_str = n.to_s
# Pad the string with leading zeroes if it is less than
# 6 digits long.
# "0" would be padded to "000000"
# "123" would be padded to "000123"
# "999999" would not be padded, and remains unchanged as "999999"
padded_str = unpadded_str.rjust(6, '0')
Docs to Ruby SecureRand, lot of cool tricks here.
Specific to this question I would say: (SecureRandom.random_number * 1000000).to_i
Docs: random_number(n=0)
If 0 is given or an argument is not given, ::random_number returns a float: 0.0 <= ::random_number < 1.0.
Then multiply by 6 decimal places (* 1000000) and truncate the decimals (.to_i)
If letters are okay, I prefer .hex:
SecureRandom.hex(3) #=> "e15b05"
Docs:
hex(n=nil)
::hex generates a random hexadecimal string.
The argument n specifies the length, in bytes, of the random number to
be generated. The length of the resulting hexadecimal string is twice
n.
If n is not specified or is nil, 16 is assumed. It may be larger in
future.
The result may contain 0-9 and a-f.
Other options:
SecureRandom.uuid #=> "3f780c86-6897-457e-9d0b-ef3963fbc0a8"
SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
For Rails apps creating a barcode or uid with an object you can do something like this in the object model file:
before_create :generate_barcode
def generate_barcode
begin
return if self.barcode.present?
self.barcode = SecureRandom.hex.upcase
end while self.class.exists?(barcode: barcode)
end
SecureRandom.random_number(n) gives a random value between 0 to n. You can achieve it using rand function.
2.3.1 :025 > rand(10**5..10**6-1)
=> 742840
rand(a..b) gives a random number between a and b. Here, you always get a 6 digit random number between 10^5 and 10^6-1.

Median of an array in Ruby not floating

OK folks...I could use some help on getting the median of an array in ruby
Here is my code:
def median(array)
array.sort! # sort the array
elements = array.count # count the elements in the array
center = elements/2 # find the center of the array
elements.even? ? (array[center] + array[center+1])/2 : array[center] # if elements are even take both the center numbers of array and divide in half, if odd...get the center number
end
Just not certain where to apply the .to_f since it wont return anything needing a float.
Thanks
I realized you've solved your own problem already, but this version is a little cleaner and safer:
def median(array)
raise ArgumentError, 'Cannot find the median on an empty array' if array.size == 0
sorted = array.sort
midpoint, remainder = sorted.length.divmod(2)
if remainder == 0 # even count, average middle two
sorted[midpoint-1,2].inject(:+) / 2.0
else
sorted[midpoint]
end
end
Use enumerable-statistics.
https://github.com/mrkn/enumerable-statistics
This gem was created by the Ruby committer as a C extension.
I think it is reliable and fast.
It Returns int if the number of array elements is odd. If the number of array elements is even, it returns a float.
require "enumerable/statistics"
arr = [1, 2, 3, 4, 5]
arr.median # => 3
arr.median.class # => Integer
arr = [1,2,3,4,5,6]
arr.median # => 3.5
Other useful methods are also available.
mean, variance, stdev, mean_variance, mean_stdev, percentile, value_counts histogram

How to best create a random float in a range between two floats

I know that I can generate random floats with rand(max). I tried to generate a float in a range, this shouldn't be hard. But e.g rand(1.4512) returns 0, thus rand isn't calculating with floats. Now I tried a little trick, converting the thing to an integer and after randomizing a fitting number in my desired range, calculating it back to a float.. which is not working.
My question is how to do this in a better way. If there is no better way, why is this one not working? (Maybe it's too late for me, I should've started sleeping 2 hours ago..). The whole thing aims to be a method for calculating a "position" field for database records so users can order them manually. I've never done something like this before, maybe someone can hint me with a better solution.
Here's the code so far:
def calculate_position(#elements, index)
min = #elements[index].position
if #elements[index + 1].nil?
pos = min + 1
else
pos = min + (rand(#elements[index + 1].position * 10000000000) / 10000000000)
end
return pos
end
Pass a range of floats to rand
If you want to "create a random float in a range between two floats", just pass a range of floats to rand.
rand(11.2...76.9)
(Tested with Ruby 2.1)
Edit
According to the documentation: https://ruby-doc.org/core-2.4.0/Random.html
There are two different ways to write the random function: inclusive and exclusive for the last value
rand(5..9) # => one of [5, 6, 7, 8, 9]
rand(5...9) # => one of [5, 6, 7, 8]
rand(5.0..9.0) # => between 5.0 and 9.0, including 9.0
rand(5.0...9.0) # => between 5.0 and 9.0, excluding 9.0
Let's recap:
rand() will generate a (psuedo-)random
float between 0 and 1.
rand(int) will generate a
(psuedo-)random integer between 0 and
int.
So something like:
def range (min, max)
rand * (max-min) + min
end
Should do nicely.
Update:
Just tested with a little unit test:
def testRange
min = 1
max = 100
1_000_000.times {
result = range min, max
print "ERROR" if result < min || result > max
}
end
Looks fine.
In 1.9 and 2.0 you can give a range argument to rand:
irb(main):001:0> 10.times { puts rand Math::E..Math::PI }
3.0656267148715446
2.7813979580609587
2.7661725184200563
2.9745784681934655
2.852157154320737
2.741063222095785
2.992638029938756
3.0713152547478866
2.879739743508003
2.7836491029737407
=> 10
I think your best bet is to use rand() to generate a random float between 0 and 1, and then multiply to set the range and add to set the offset:
def float_rand(start_num, end_num=0)
width = end_num-start_num
return (rand*width)+start_num
end
Note: since the order of the terms doesn't matter, making end_num default to 0 allows you to get a random float between 0 and x with float_rand(x).

Resources