Really small numbers in ruby coming as zero!?

M

Mahadev Ittina

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!
 
A

Alex Young

Mahadev said:
# 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.
 
M

Marnen Laibow-Koser

Alex said:
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.

Best,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top