swift-rationals

Rationals is a package containing Fraction and Percentage types for the Swift programming language.

Contents

The package currently provides the following extensions:

BinaryFloatingPoint

  • isInteger: A boolean value indicating whether this instance is an integer.
  • integral: The integral part of this instance.
  • fractional: The fractional part of this instance.
  • decimal: The decimal part of this instance.
  • totalDigits: The number of total digits in this instance.
  • decimalPlaces: The number of decimal places in this instance.
  • truncated(to:): Returns this instance truncated to the specified decimal place.
  • truncate(to:): Truncates this instance to the specified decimal place.

And the following implementations:

Fraction: A representation of a fraction, or the quotient of two numbers without loss of precision.

  • init(_:on:): Creates a new instance with the specified numerator and denominator.
  • init(_:and:on:): Creates a new instance with the specified integral, numerator and denominator.
  • init(approximately:withPrecision:): Creates a new instance with the specified approximate value.
  • isDyadic: A boolean value indicating whether this fraction is dyadic.
  • isImproper: A boolean value indicating whether this fraction is improper.
  • isMixed: A boolean value indicating whether this fraction is improper.
  • isNormalized: A boolean value indicating whether this fraction is normalized.
  • isProper: A boolean value indicating whether this fraction is proper.
  • isSimplified: A boolean value indicating whether this fraction is simplified.
  • isUnit: A boolean value indicating whether this fraction is a unit.
  • isWhole: A boolean value indicating whether this instance is whole.
  • isEquivalent(to:): Returns a boolean value indicating whether the two specified fractions are equivalent.
  • isLike(_:): Returns a boolean value indicating whether the two specified fractions are like fractions.

Simplified: A property wrapper that keeps a fraction at its lowest terms.

Percentage: A representation of a percentage, or the quotient of a fraction with a denominator of 100.

  • init(_:): Creates a new instance with the specified numerator.

Increasable: Representing values that can be increased by n percent.

  • increasing(by:): Returns this instance increased by the specified value.
  • increase(by:): Increases this instance by the specified value and produces their sum.

Decreasable: Representing values that can be decreased by n percent.

  • decreasing(by:): Returns this instance decreased by the specified value.
  • decrease(by:): Decreases this instance by the specified value and produces their difference.

Installation

To use this package in a SwiftPM project:

  1. Add it to the dependencies in your Package.swift file:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/alexandrehsaad/swift-rationals.git", from: "main")
    ],
    ...
)
  1. Add it as a dependency for your target in your Package.swift file:

let package = Package(
    ...
    targets: [
        .target(name: "MyTarget", dependencies: [
            .product(name: "Rationals", package: "swift-rationals")
        ]),
    ],
    ...
)
  1. Import the package in your source code.

import Rationals

Contribution

Reporting a bug

If you find a bug, please open a bug report.

Contacting the maintainers

The current code owner of this package is Alexandre H. Saad (@alexandrehsaad). You can contact him by writing an email to alexandresaad at icloud dot com.

Supporting

If you like our work, show your support by staring this repository.

Feedback

We would love to hear your thoughts or feedback on how we can improve Swift Rationals!

GitHub

View Github