3therk1ll
3therk1ll

Reputation: 2421

Floating point integers in Ruby

Trying to make a small script to calculate the incrementally increasing value of a base number by 2%. Having trouble with, I think, the way I'm handling the floating point. The script should calculate each number up to a preset value, but it just goes on infinitely.

    require 'bigdecimal'


def multiplication sum, count
    print "Original Sum: #{sum}\n"
    until sum == 100 do

        float = BigDecimal('1.02')

        next_sum = (sum * float.round(3))
        print "#{count}: #{next_sum}\n"

        count += 1
        sum = next_sum
    end
end

multiplication 2, 1

Upvotes: 1

Views: 103

Answers (1)

samgak
samgak

Reputation: 24417

Your script is looping until sum is exactly 100, which might not happen if it jumps from a value less to a value greater. Change the loop condition to this:

until sum >= 100 do

Also, "floating point integer" is a contradiction in terms.

Upvotes: 4

Related Questions