Git Product home page Git Product logo

swift-rational's Introduction

Swift Rational

Introduction

Swift Rational provides the RationalModule module for working with rational numbers in Swift.

import RationalModule

let half = Rational<Int>(1, 2)

RationalModule has only a single dependency, swift-numerics.

Using Swift Rational in your project

To use Swift Rational in a SwiftPM project:

  1. Add the following line to the dependencies in your Package.swift file:
.package(url: "https://github.com/abdel-17/swift-rational", from: "1.0.0")
  1. Add RationalModule as a dependency for your target:
.target(
	name: "TargetName",
	dependencies: [
		.product(name: "RationalModule", package: "swift-rational")
	]
)
  1. Add import RationalModule in your source code.

API

RationalModule exports the Rational struct. It conforms to standard Swift protocols like AdditiveArithmetic, Numeric, Hashable, Comparable, and more.

You can create a Rational value using the fraction initializer.

let half = Rational(2, 4)
print(x.numerator)	// 1
print(x.denominator)	// 2

You can also use the integer initializer.

let one = Rational(1)

Or simply an integer literal.

let two: Rational<Int> = 2

Rational supports the standard arithmetic and comparison operators.

Rational(1, 2) + Rational(1, 4)		// Rational(3, 4)
Rational(1)    - Rational(1, 2)		// Rational(1, 2)
Rational(2)    * Rational(3, 4)		// Rational(3, 2)
Rational(1)    / Rational(1, 2)		// Rational(2, 1)
Rational(1, 2) < Rational(3, 4)		// true

Attributions

A lot of the implementations were ported over to Swift from Python's fractions module.

swift-rational's People

Contributors

abdel-17 avatar maartene avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

maartene

swift-rational's Issues

limitDenominator traps because it tries to create a Rational with a negative denominator

Hi,

While writing test cases for I found it traps for instance when using the following example case:

let r = Rational(-65799040, 325053649)
print(r.limitDenominator(to: 10_000_000)).    // traps because it tries to create a rational with a negative denominator

For comparison: the resulting rational should be Rational(-233695, 1154476) (as per Pythons Fraction module)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.