Qqwy

Qqwy

TypeCheck Core Team

Ratio - rational numbers in Elixir

Ratio

Ratio is a library to work with rational numbers (also known as ‘fractions’) in Elixir.

hex.pm version Documentation ci


A bit of history

Ratio has been around for a very long time. It was the first library I wrote when I started learning Elixir, with the first public release happening in March of 2016. (For Elixir v1.2. Oh, how the time flies!)

Throughout the years, my onlook on programming has changed a little, Elixir has changed a little, and the library changed to match.

From the start, Ratio allowed to use the <|> operator as shorthand notation to allow for a natural syntax like 1 <|> 2. Ratio also supported overloaded math operators, for the same reason.

This operator overloading was later moved to an ‘opt-in’ approach. Later, support was added to mix-and-match overloaded math operators for different kinds of number types, by extracting this functionality to the Numbers library.

Recently it came to my attention that <|> has been deprecated starting in Elixir v1.14.
And so, the library again had to slightly change.

Today

And this brings me to today. A release candidate for v4.0.0 of the library has been published to Hex.PM just now.
The important (backwards-incompatible) changes to v3.0.2 are:

  • Remove infix operator <|> as its usage is deprecated in Elixir v1.14.
  • Switch the Inspect implementation to use the form Ratio.new(10, 20) instead of 10 <|> 20.
  • Remove implementation of String.Chars, as the old implementation was not in a (non-programmer) human-readable format.

You can still use <|> or <~> (which is not deprecated in Elixir v1.14 and should stay around for a long time yet to come), by writing

defdelegate numerator <~> denominator, to: Ratio, as: :new

in your module.

For instance, here is an example of if you want to use the full opt-in syntactic sugary goodness:

defmodule IDoAlotOfMathHere do
  defdelegate numerator <~> denominator, to: Ratio, as: :new
  use Numbers, overload_operators: true

  def calculate(input) do
     num = input <~> 2
     result = num * 2 + (3 <~> 4) * 5.0
     result / 2
  end
end

iex> IDoAlotOfMathHere.calculate(42)
Ratio.new(183, 8)

Please try out the new release candidate of the library (by adding {:ratio, "4.0.0-rc.0"} to your deps), and let me know if you encounter any bugs or oddities!

~Marten / Qqwy

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

Release candidate 4.0.0-rc.1 has been released, which contains a fix for a regression where constructing a rational number from a decimal did not work.

Where Next?

Popular in Announcing Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14410 106
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19951 141
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement