Really small numbers in ruby coming as zero!?

hello I have this piece of code written to analyse the cross section and
give the reinforcement for a concrete beam..

#initializing some constants.. pls ignore this part!
M = 340000000
h = 630
b = 300
fck = 40
    d = b-30

# This is where I am having my problem
    K = ((M) / (b*(d**2)*fck)).to_f

# I have defined other constants.. but didn't wanna crowd the question
    z = (d/2) * (1 + Math.sqrt(1-2*(K/kappa)))

area_steel = (M) / (fyd*z)
puts "Area of Steel Required " + area_steel.to_s + " sq. mm"

*end of code*

thanks for your help cheers!

···

--
Posted via http://www.ruby-forum.com/.

Mahadev Ittina wrote:

# This is where I am having my problem
    K = ((M) / (b*(d**2)*fck)).to_f

K = M.to_f / (b*(d**2)*fck)

In your code, you're dividing an integer by an integer, so Ruby makes it
an integer division. If you make either the numerator or denominator a
float, you'll get a float result.

···

--
Alex
--
Posted via http://www.ruby-forum.com/\.

Thanks really appreciate it.. :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Alex Young wrote:

Mahadev Ittina wrote:

# This is where I am having my problem
    K = ((M) / (b*(d**2)*fck)).to_f

K = M.to_f / (b*(d**2)*fck)

In your code, you're dividing an integer by an integer, so Ruby makes it
an integer division. If you make either the numerator or denominator a
float, you'll get a float result.

But don't use floats for math, because of the imprecision inherent in
the format. For math, stick to integers or BigDecimal.

--
Alex

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.