0

I am a Ruby beginner currently working on learning how to work with RSpec. I am also working on a temperature converter and I have gotten it to pass the RSpec tests for my ftoc (Fahrenheit to Celsius) but I am having problems trying to pass the last test of my ctof function. When "body temperature" is passed through my ctof method, it is expected to return a value be_within(0.1).of(98.6) and instead I have been only able to make it return 98 and the test doesn't pass. How does the be_within work? how can I get the desired value (be_within(0.1).of(98.6)) without affecting my other tests?

Here my code:

def ftoc(fahrenheit_degrees)
  celsius = (fahrenheit_degrees.to_i - 32) * 5.0 / 9.0
  celsius.round
end

def ctof(celsius_degrees)
  fahrenheit = (celsius_degrees.to_i * 9 / 5) + 32
  fahrenheit.round
end

Here my RSpec code :

describe "temperature conversion functions" do
  describe "#ftoc" do
    it "converts freezing temperature" do
      expect(ftoc(32)).to eq(0)
    end

    it "converts boiling temperature" do
      expect(ftoc(212)).to eq(100)
    end

    it "converts body temperature" do
      expect(ftoc(98.6)).to eq(37)
    end

    it "converts arbitrary temperature" do
      expect(ftoc(68)).to eq(20)
    end
  end

  describe "#ctof" do
    it "converts freezing temperature" do
      expect(ctof(0)).to eq(32)
    end

    it "converts boiling temperature" do
      expect(ctof(100)).to eq(212)
    end

    it "converts arbitrary temperature" do
      expect(ctof(20)).to eq(68)
    end

    it "converts body temperature" do
      expect(ctof(37)).to be_within(0.1).of(98.6)  #Here my problem  :/
    end 
  end
end
mikej
  • 65,295
  • 17
  • 152
  • 131
Monique
  • 279
  • 3
  • 20

2 Answers2

2

Here are some thoughts on your code:

  • you should not be calling round on the result; you may want to use round(1) to round it to 1 decimal place.

  • you should not be calling to_i on your temperatures -- doing so, you are losing information that is needed for the conversion (e.g. 98.6 would be converted to 98, 97.9 to 97).

  • Your ctof was using 9 / 5 which is an integer operation that will resolve to 1. You need to specify at least one of those operands as a floating point number (as you did in ftoc) (by adding .0 to it) to guarantee that it will be a floating point (and not an integer) calculation.

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
0

Your problem appears to be that you're doing integer division instead of floating point division in ctof:

37 * 9 / 5 + 32
# => 98
37 * 9.0 / 5 + 32
# => 98.6

Your doing this correctly in ftoc and is one of the weirder things to have to get used to in programming, dividing 2 integers will never return a floating point (decimal) number. Have to always make sure at least one of them is floating point.

You also probably don't want to round these values on the second line of the method (fahrenheit.round), or at least want to pass a number of decimal places to round to:

(37 * 9.0 / 5 + 32).round
# => 99
(0.12345).round(3)
# => 0.123
(0.12345).round(1)
# => 0.1

Also, have a look through the answers to this other question for some more details and a couple of alternative solutions.

Simple Lime
  • 10,790
  • 2
  • 17
  • 32
  • Thank you Simple Lime for your help and explanations. I will research a bit more on it but this helped me to solve my issue! – Monique Jan 08 '18 at 16:07