0

我正在使用 Ruby 和 Latex 为寄存器创建颜色编码集。我有以下代码块。当试图运行它时,band1=1e+02.

我试过band1= (BigDecimal(i) * 100).to_f,想也许有一些奇怪的浮点问题。一个整数乘以一个整数应该创建一个整数。我也尝试了各种其他方法,但无济于事。

(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

不知道我错过了什么。我应该each_with_index通过这些迭代使用吗?我试过这个:

(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

我得到了同样的答案。我不明白为什么1*100应该等同于这么大的数字。

编辑:附加信息:如果我有这个:band1=i*10 那么计算是正确的。事实上,计算正确到 99。

4

1 回答 1

0

在您的代码中,band1必须是Fixnum. 检查与p band1.class。不知道你是如何得到“1e+02”的,也许你以某种奇怪的方式打印,或者你这样做是在 Rubyband1 == 1e+02中返回的。true您必须使用和eql?来区分:11.0

1 ==   1.0  # => true
1.eql?(1.0) # => false
于 2012-11-07T05:40:18.997 回答