Ratio.FloatConversion (ratio v2.4.2) View Source

Link to this section Summary

Functions

Converts a float to a rational number. Because base-2 floats cannot represent all base-10 fractions properly, the results might be different from what you might expect. See The Perils of Floating Point for more information about this.

Link to this section Functions

Link to this function

float_to_rational(float, max_decimals \\ 10)

View Source

Converts a float to a rational number. Because base-2 floats cannot represent all base-10 fractions properly, the results might be different from what you might expect. See The Perils of Floating Point for more information about this.

It is possible to restrict max_decimals to make the result more readable/understandable, at the possible loss of precision. The default value for max_decimals is 10 (Erlang allows values between 0 and 249, see erlang's float_to_list function)

Examples

iex> Ratio.FloatConversion.float_to_rational(10.0)
10
iex> Ratio.FloatConversion.float_to_rational(13.5)
27 <|> 2
iex> Ratio.FloatConversion.float_to_rational(1.1, 100)
2476979795053773 <|> 2251799813685248
iex> Ratio.FloatConversion.float_to_rational(1.1, 3)
11 <|> 10