Skip to content

Commit

Permalink
Added simplified BigDecimal functions to enable easier Real protoco…
Browse files Browse the repository at this point in the history
…l compliance.
  • Loading branch information
mgriebling committed Jul 10, 2023
1 parent 3dbbe2a commit cf2ed84
Show file tree
Hide file tree
Showing 759 changed files with 960 additions and 794 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let package = Package(
// .macOS("13.3"), .iOS("16.4"), .macCatalyst(.v16), .tvOS("16.4"),
// .watchOS("9.4")
// ],
// commented out due to playground incompatibility
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand All @@ -19,7 +20,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/mgriebling/BigInt.git", from: "2.0.0"),
.package(url: "https://github.com/mgriebling/UInt128.git", from: "3.0.0")
// .package(url: "https://github.com/apple/swift-numerics", from: "1.0.0") // playground incompatible
// .package(url: "https://github.com/apple/swift-numerics", from: "1.0.0") // playground incompatible
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ limit for performance) and fixed-precision decimal arithmetic in Swift.
Its functionality falls in the following categories:
- Arithmetic: addition, subtraction, multiplication, division, remainder and
exponentiation
- Constants: pi, zero, one, ten
- Compliant with `DecimalFloatingPoint` and, optionally, `Real` protocols.
- Support for complex decimal numbers via ` if `Real` protocol compliance is enabled.
- Constants: `pi`, `zero`, `one`, `ten`
- Functions: exp, log, log10, log2, pow, sqrt, root, factorial, gamma,
trig + inverse, hyperbolic + inverse
- Rounding and scaling according to one of the rounding modes:
Expand All @@ -20,6 +22,7 @@ Its functionality falls in the following categories:
- toNearestOrEven
- toNearestOrAwayFromZero
- up

- Comparison: the six standard operators `==`, `!=`, `<`, `<=`, `>`, and `>=`
- Conversion: to String, to Double, to Decimal (the Swift Foundation type), to
Decimal32 / Decimal64 / Decimal128
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"primaryContentSections":[{"kind":"declarations","declarations":[{"tokens":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"languages":["swift"],"platforms":["macOS"]}]}],"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"variants":[{"paths":["\/documentation\/bigdecimal\/asincalculator\/calculateinpairs"],"traits":[{"interfaceLanguage":"swift"}]}],"identifier":{"url":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateInPairs","interfaceLanguage":"swift"},"abstract":[{"type":"text","text":"Inherited from "},{"type":"codeVoice","code":"SeriesCalculator.calculateInPairs"},{"type":"text","text":"."}],"kind":"symbol","metadata":{"fragments":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"title":"calculateInPairs","roleHeading":"Instance Property","role":"symbol","symbolKind":"property","externalID":"s:10BigDecimal14AsinCalculatorV16calculateInPairsSbvp","modules":[{"name":"BigDecimal"}]},"hierarchy":{"paths":[["doc:\/\/BigDecimal\/documentation\/BigDecimal","doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator"]]},"references":{"doc://BigDecimal/documentation/BigDecimal":{"role":"collection","title":"BigDecimal","abstract":[{"type":"text","text":"The BigDecimal package provides arbitrary-precision (with an adjustable upper"},{"type":"text","text":" "},{"type":"text","text":"limit for performance) and fixed-precision decimal arithmetic in Swift."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator/calculateInPairs":{"role":"symbol","title":"calculateInPairs","fragments":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"abstract":[],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateInPairs","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal\/asincalculator\/calculateinpairs"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator":{"role":"symbol","title":"AsinCalculator","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"AsinCalculator"}],"abstract":[{"type":"text","text":"Calculates arcsine using the Maclaurin\/Taylor series."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"AsinCalculator"}],"url":"\/documentation\/bigdecimal\/asincalculator"}}}
{"primaryContentSections":[{"kind":"declarations","declarations":[{"tokens":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"languages":["swift"],"platforms":["macOS"]}]}],"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"variants":[{"paths":["\/documentation\/bigdecimal\/asincalculator\/calculateinpairs"],"traits":[{"interfaceLanguage":"swift"}]}],"identifier":{"url":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateInPairs","interfaceLanguage":"swift"},"abstract":[{"type":"text","text":"Inherited from "},{"type":"codeVoice","code":"SeriesCalculator.calculateInPairs"},{"type":"text","text":"."}],"kind":"symbol","metadata":{"fragments":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"title":"calculateInPairs","roleHeading":"Instance Property","role":"symbol","symbolKind":"property","externalID":"s:10BigDecimal14AsinCalculatorV16calculateInPairsSbvp","modules":[{"name":"BigDecimal"}]},"hierarchy":{"paths":[["doc:\/\/BigDecimal\/documentation\/BigDecimal","doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator"]]},"references":{"doc://BigDecimal/documentation/BigDecimal/AsinCalculator/calculateInPairs":{"role":"symbol","title":"calculateInPairs","fragments":[{"kind":"keyword","text":"var"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateInPairs"},{"kind":"text","text":": "},{"kind":"typeIdentifier","text":"Bool","preciseIdentifier":"s:Sb"}],"abstract":[],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateInPairs","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal\/asincalculator\/calculateinpairs"},"doc://BigDecimal/documentation/BigDecimal":{"role":"collection","title":"BigDecimal","abstract":[{"type":"text","text":"The BigDecimal package provides arbitrary-precision (with an adjustable upper"},{"type":"text","text":" "},{"type":"text","text":"limit for performance) and fixed-precision decimal arithmetic in Swift."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator":{"role":"symbol","title":"AsinCalculator","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"AsinCalculator"}],"abstract":[{"type":"text","text":"Calculates arcsine using the Maclaurin\/Taylor series."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"AsinCalculator"}],"url":"\/documentation\/bigdecimal\/asincalculator"}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"primaryContentSections":[{"kind":"declarations","declarations":[{"tokens":[{"kind":"keyword","text":"mutating"},{"kind":"text","text":" "},{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"languages":["swift"],"platforms":["macOS"]}]}],"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"variants":[{"paths":["\/documentation\/bigdecimal\/asincalculator\/calculatenextfactor()"],"traits":[{"interfaceLanguage":"swift"}]}],"identifier":{"url":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateNextFactor()","interfaceLanguage":"swift"},"abstract":[{"type":"text","text":"Calculates the factor of the next term."}],"kind":"symbol","metadata":{"fragments":[{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"title":"calculateNextFactor()","roleHeading":"Instance Method","role":"symbol","symbolKind":"method","externalID":"s:10BigDecimal14AsinCalculatorV19calculateNextFactoryyF","modules":[{"name":"BigDecimal"}]},"hierarchy":{"paths":[["doc:\/\/BigDecimal\/documentation\/BigDecimal","doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator"]]},"references":{"doc://BigDecimal/documentation/BigDecimal":{"role":"collection","title":"BigDecimal","abstract":[{"type":"text","text":"The BigDecimal package provides arbitrary-precision (with an adjustable upper"},{"type":"text","text":" "},{"type":"text","text":"limit for performance) and fixed-precision decimal arithmetic in Swift."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator/calculateNextFactor()":{"role":"symbol","title":"calculateNextFactor()","fragments":[{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"abstract":[{"type":"text","text":"Calculates the factor of the next term."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateNextFactor()","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal\/asincalculator\/calculatenextfactor()"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator":{"role":"symbol","title":"AsinCalculator","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"AsinCalculator"}],"abstract":[{"type":"text","text":"Calculates arcsine using the Maclaurin\/Taylor series."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"AsinCalculator"}],"url":"\/documentation\/bigdecimal\/asincalculator"}}}
{"primaryContentSections":[{"kind":"declarations","declarations":[{"tokens":[{"kind":"keyword","text":"mutating"},{"kind":"text","text":" "},{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"languages":["swift"],"platforms":["macOS"]}]}],"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"variants":[{"paths":["\/documentation\/bigdecimal\/asincalculator\/calculatenextfactor()"],"traits":[{"interfaceLanguage":"swift"}]}],"identifier":{"url":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateNextFactor()","interfaceLanguage":"swift"},"abstract":[{"type":"text","text":"Calculates the factor of the next term."}],"kind":"symbol","metadata":{"fragments":[{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"title":"calculateNextFactor()","roleHeading":"Instance Method","role":"symbol","symbolKind":"method","externalID":"s:10BigDecimal14AsinCalculatorV19calculateNextFactoryyF","modules":[{"name":"BigDecimal"}]},"hierarchy":{"paths":[["doc:\/\/BigDecimal\/documentation\/BigDecimal","doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator"]]},"references":{"doc://BigDecimal/documentation/BigDecimal/AsinCalculator/calculateNextFactor()":{"role":"symbol","title":"calculateNextFactor()","fragments":[{"kind":"keyword","text":"func"},{"kind":"text","text":" "},{"kind":"identifier","text":"calculateNextFactor"},{"kind":"text","text":"()"}],"abstract":[{"type":"text","text":"Calculates the factor of the next term."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator\/calculateNextFactor()","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal\/asincalculator\/calculatenextfactor()"},"doc://BigDecimal/documentation/BigDecimal":{"role":"collection","title":"BigDecimal","abstract":[{"type":"text","text":"The BigDecimal package provides arbitrary-precision (with an adjustable upper"},{"type":"text","text":" "},{"type":"text","text":"limit for performance) and fixed-precision decimal arithmetic in Swift."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal","kind":"symbol","type":"topic","url":"\/documentation\/bigdecimal"},"doc://BigDecimal/documentation/BigDecimal/AsinCalculator":{"role":"symbol","title":"AsinCalculator","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"AsinCalculator"}],"abstract":[{"type":"text","text":"Calculates arcsine using the Maclaurin\/Taylor series."}],"identifier":"doc:\/\/BigDecimal\/documentation\/BigDecimal\/AsinCalculator","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"AsinCalculator"}],"url":"\/documentation\/bigdecimal\/asincalculator"}}}
Loading

2 comments on commit cf2ed84

@Lucca-mito
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive stuff. I see that the RealModule import is temporarily commented out; are there plans for BigDecimal to conform to Real out-of-the box?

@mgriebling
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, provided that the swift-numerics becomes compatible with swift playgrounds.
At the moment playgrounds do not support compiling C code which swift-numerics still has.
However, it is easy to make DecimalMath compliant to Real since it already contains all required methods:

import RealModule
import ComplexModule
import BigDecimal

extension BigDecimal : Real {
    // Add `Real` protocol support implicitly - not in original `BigDecimal`
}

Please sign in to comment.