# Really small numbers in ruby coming as zero!?

**URL:** <https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237>\
**Category:** Ruby\
**Created:** [October 31, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237 "2009-10-31T13:54:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [October 31, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237/1 "2009-10-31T13:54:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [October 31, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237/2 "2009-10-31T14:31:19Z")

</div>

Mahadev I. 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

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [October 31, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237/3 "2009-10-31T17:30:26Z")

</div>

Thanks really appreciate it… 🙂

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [October 31, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/really-small-numbers-in-ruby-coming-as-zero/178237/4 "2009-10-31T19:59:24Z")

</div>

Alex Y. wrote:

> Mahadev I. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
