https://github.com/entonio/nuggle
Combine integer and floating point math without conversions
https://github.com/entonio/nuggle
floating-point integer-arithmetic
Last synced: 3 months ago
JSON representation
Combine integer and floating point math without conversions
- Host: GitHub
- URL: https://github.com/entonio/nuggle
- Owner: entonio
- License: apache-2.0
- Created: 2024-04-11T01:24:37.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-01T03:36:56.000Z (11 months ago)
- Last Synced: 2025-02-10T07:16:56.315Z (3 months ago)
- Topics: floating-point, integer-arithmetic
- Language: Swift
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nuggle
Common compiled languages keep a visible separation between integer and floating point numbers. The purpose of this package is to allow performing mathematical operations without having to do type conversions:
- where possible / reasonable, floating point numbers are converted to integers;
- operations that require converting integers to floating point are deferred until a native numerical value is requested.NOTE: Support for radicals is very experimental.
For the reasons outlined at https://forums.swift.org/t/why-does-swift-not-support-rational-numbers/62725, this kind of abstraction isn't feasible in a general-purpose useful way. Noticeably, the performance penalty and the increased frequency of overflow issues make it applicable only in a subset of circumstances. You should kee that in mind if choosing this package for some specific use.
Goals of this package:
- ease of use
- correctness
- preserving integer mathNot goals:
- performance
- completeness of mathematical API# Examples
```swift
let m: Nuggle = 2.5
let n: Nuggle = 1.5
let p: Nuggle = 1let ex: Nuggle = 2.5 + 1.5 + 1
XCTAssertEqual(m + n + p, ex)
XCTAssertEqual(m + n, 4)
XCTAssertEqual(m + n + p, 5)
XCTAssertEqual(m + n + p, 4 + 1.6/4.8 + 2/3)XCTAssertEqual(m / n, 5 / 3)
XCTAssertEqual((m / n).description, "5/3")XCTAssertEqual(ex.exactInt(), 5)
XCTAssertEqual((m * 3).double(), 7.5)
```## License
Except where/if otherwise specified, all the files in this package are copyright of the package contributors mentioned in the `NOTICE` file and licensed under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0), which is permissive for business use.