Calculations on the iteration count in for loop in Ruby 1.8.7
Posted
by
user1805035
on Stack Overflow
See other posts from Stack Overflow
or by user1805035
Published on 2012-11-07T04:58:22Z
Indexed on
2012/11/07
4:59 UTC
Read the original article
Hit count: 149
I was playing around with Ruby and Latex to create a color coding set. I'm more than a novice with C++, but haven't looked at Ruby until now. So, still learning a lot of coding.
I have the following block of code below. When attempting to run this, band1 = 1e+02.
I've tried band1 = (BigDecimal(i) * 100).to_f thinking maybe there was some odd floating point issue. This is just me trying anything though as an integer multiplied by an integer should create an integer, if I'm still thinking correctly. I've tried a variety of other things as well (things that I can do in C++, but this ain't C++), but to no avail.
(1..9).each do |i| #Band 1
(0..9).each do |j| #Band 2
(0..11).each do |k| #Band 3
#Band 3 Start
#these are the colors of the resistor bands
b1 = $c_band12[i]
b2 = $c_band12[j]
b3 = $c_band3[k]
b4 = "Gold"
oms = ((i*100) + (j*10)) * $mult[k]
band1 = i*100
band2 = j
band3 = $mult[k]
end
end
end
Not sure what I'm missing. Should I be using .each_with_index through these iterations? I've tried this: (1..9).each_with_index {|i, indexi| #Band 1 (0..9).each_with_index {|j, indexj| #Band 2 (0..11).each_with_index {|k, indexk| #Band 3 #Band 3 Start #these are the colors of the resistor bands b1 = $c_band12[i] b2 = $c_band12[j] b3 = $c_band3[k] b4 = "Gold"
oms = ((i*100) + (j*10)) * $mult[k]
band1 = indexk * 100
and I get the same answer. I can't see why 1*100 should equate to such a large number?
Thank you, AT
© Stack Overflow or respective owner