Crates.io | floating_bar |
lib.rs | floating_bar |
version | 0.4.0 |
source | src |
created_at | 2019-01-21 00:27:23.837705 |
updated_at | 2021-04-13 06:17:04.514264 |
description | Representing rational numbers using the floating-bar number type. |
homepage | |
repository | https://github.com/1011X/floating_bar |
max_upload_size | |
id | 109744 |
size | 53,449 |
This library provides the floating-bar type, which gives a memory-efficient representation for rational numbers. It is based on Inigo Quilez's blog post exploring the concept.
For more information about the API and implementation details, please refer to the library documentation.
Almost all programming languages provide a way to represent different numeric types. These usually include natural numbers (u16
), integers (i32
), and reals (f64
). However, there are no numeric types that cover rational numbers. The purpose of this library is to fulfill this niche not covered by other numeric types.
Although floating-point numbers can usually serve as a good enough substitute for rational numbers, this comes at the slight cost of precision due to the way they're encoded. This is immediately apparent when evaluating expressions that should be zero like 3/6 + 2/6 - 5/6
, which returns -1.1102230246251565e-16
with double precision. Thus, floating-point numbers aren't always a good way to represent rational numbers.
Floating-bar numbers avoid this issue by storing the exact integer values in a compact format without losing precision. If your calculations involve handling fractions or dividing integers in general, this library will be a great fit.
Conversely, this library is not meant as a replacement to floating-point numbers, just like rational numbers are not a replacement for real numbers. Floating-point is still beneficial when doing calculations where irrational numbers are involved (like tau, pi, e, or square roots), or when an approximate result is considered good enough.
That said, they're not mutually exclusive. There are conversion methods to convert from r32
to f32
when needed (and vice-versa).
std
(default)This enables use of std
when necessary. In particular, this implements std::error::Error
for ParseRatioErr
.
This feature is enabled by default.
Pull requests are welcome! Feel free to look at the issue list on GitHub for something you can work on. You can also submit an issue for a feature that would be useful to you.
This project is licensed under the BSD Zero Clause License.