Integer
s hold any number no matter how big, up to the limit of your machine's memory, while Int
corresponds to the set of positive and negative integers that can be expressed in 32 or 64 bits (guaranteed at least -2^29 to 2^29).
Float
corresponds to the set of real numbers, limited by the precision of the computer.
Operations defined on numbers usually work on one type or the other, but not both.
Functions to convert between Float
and Integer
include, among others, toFloat
which converts Int
/Integer
to Float
and ceiling
which converts a Float
to an Int
.
Conversion from an integer type (Int
or Integer
) to any other numeric type is done using the function fromIntegral
.
The target type is inferred automatically.
For example:
n :: Integer
n = 6
x :: Float
x = fromIntegral n --> x = 6.0
m :: Int
m = 7
y :: Double
y = fromIntegral m --> y = 7.0
You are part of a firefighter organization made of international volunteers. Due to the international nature of the team, valuable time has been wasted over the last few calls translating between Fahrenheit and Celcius while estimating the temperature inside the burning buildings. To save time, and people, you decide to create a program to automatically convert between Fahrenheit and Celsius for your international team.
Implement the tempToC
function to convert an Integer
temperature in Fahrenheit to its equivalent Float
in Celsius rounded to the nearest hundredth.
Temperature in degrees Celsius is defined as: T(°C) = (T(°F) - 32) / 1.8.
> tempToC 32
0.00
Implement the tempToF
function to convert a Float
temperature in Celsius to its equivalent in Fahrenheit.
For safety reasons, round up the result to the next-highest Integer
.
Temperature in degrees Fahrenheit is defined as: T(°F) = T(°C) × 1.8 + 32.
> tempToF 4
40
Sign up to Exercism to learn and master Haskell with 107 exercises, and real human mentoring, all for free.