Qqwy

Qqwy

TypeCheck Core Team

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

Showing Posts 1 to 1

Qqwy

Qqwy OP

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.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews