Unexpected Ruby Float behavior during multiplication [duplicate] - ruby

This question already has answers here:
ruby floating point errors
(3 answers)
Closed 7 years ago.
30.0 * 0.009 # => 0.26999999999999996
Does anybody know why this is not 0.27?
Any recommendations on what to do about this?

Since you already found materials on the why subject, I will just answer the what to do part of the question. If you absolutely don't want to approximate, you have to use rational numbers:
30 * 0.009r # => (27/100)

Related

Reduction vs MapToInt to obtain sum in Streams API [duplicate]

This question already has answers here:
Java mapToInt vs Reduce with map
(2 answers)
Is there any advantage of calling map after mapToInt, where ever required
(3 answers)
Closed 3 years ago.
Following streams produces the same result however there are two different ways to achieve this.
Person.stream().map(Person::getAge).reduce(Integer::sum).get();
Person.stream().mapToInt( Person::getAge).sum();
I want to know difference among two and which one is better.

using rand method in ruby for computer vs player [duplicate]

This question already has answers here:
How do I pick randomly from an array?
(7 answers)
Closed 8 years ago.
I am trying to understand how to use rand to do this. if there were two players for example player a and player b and the computer had to be one and the human the other. How would i implement this?
[:computer, :player].shuffle.first will give you on random who is first.

multiplication behavior in ruby [duplicate]

This question already has answers here:
Floating point error in representation?
(2 answers)
Closed 9 years ago.
Regarding multiplication, behavior of ruby is that 24.0 * 0.1 is not 2.4. Why does this happen?
24.0 * 0.1
# => 2.4000000000000004
Only very few numbers can be represented precisely in floating point arithmetic.
See http://en.wikipedia.org/wiki/Floating_point for more details.

Is there a confidence score like property in libsvm? [duplicate]

This question already has answers here:
Classification score: SVM
(2 answers)
Closed 8 years ago.
I am using libsvm python interface and wondering is there a way to get a confidence score/probability when using multi-classification
I'd recommend the scikit-learn wrapper of libSVM which incoroporates a predict_proba function and a score function http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.

What is the difference between ".scan" and ".split" in Ruby language? [duplicate]

This question already has an answer here:
Difference between String.scan and String.split
(1 answer)
Closed 9 years ago.
What is the difference between ".scan" and ".split" in Ruby language?
Can someone give me an example?
i am really got confused about it!
scan extracts substrings that match the given regex.
split extracts substrings that do not match the given regex (and optionally, substrings that match as well).

Resources