Skip to content

Commit 496a58c

Browse files
committed
BREAKING: Make .c namespace API more Swifty
Make ergonomic stdlib extensions idiomatic Swift while keeping authoritative ISO_9899.Math implementations literal (unchanged). ## API Changes ### Category 1: Remove C Type Prefixes - `fabs` → `abs` - `fmin(_:)` → `min(_:)` - `fmax(_:)` → `max(_:)` - `fmod(_:)` → `mod(_:)` - `fdim(_:)` → `positiveDifference(from:)` ### Category 2: Classification Functions - `fpclassify` → `classification` - `isfinite` → `isFinite` (camelCase) - `isinf` → `isInfinite` (camelCase) - `isnan` → `isNaN` (camelCase) - `isnormal` → `isNormal` (camelCase) - `signbit` → `hasNegativeSign` ### Category 3: Comparison Functions (with Swift-style labels) - `isgreater(_:)` → `isGreater(than:)` - `isgreaterequal(_:)` → `isGreaterOrEqual(to:)` - `isless(_:)` → `isLess(than:)` - `islessequal(_:)` → `isLessOrEqual(to:)` - `islessgreater(_:)` → `isNotEqual(to:)` - `isunordered(_:)` → `isUnordered(with:)` ### Category 4: Integer-Returning Rounding - `lrint` → `roundedToInt()` - `llrint` → `roundedToInt64()` - `lround` → `roundToInt()` - `llround` → `roundToInt64()` ### Category 5: Sign Manipulation - `copysign(_:)` → `withSign(of:)` - `nextafter(_:)` → `nextRepresentable(toward:)` - `nexttoward(_:)` → `nextRepresentableExtended(toward:)` ## Migration Guide Before: ```swift let abs = (-x).c.fabs let max = x.c.fmax(y) let finite = x.c.isfinite let greater = x.c.isgreater(y) let asInt = x.c.lround ``` After: ```swift let abs = (-x).c.abs let max = x.c.max(y) let finite = x.c.isFinite let greater = x.c.isGreater(than: y) let asInt = x.c.roundToInt() ``` Authoritative API unchanged: ```swift ISO_9899.Math.fabs(-2.0) // Still works ISO_9899.Math.fmax(x, y) // Still works ``` All tests passing (175/175).
1 parent 456a0b1 commit 496a58c

File tree

3 files changed

+197
-111
lines changed

3 files changed

+197
-111
lines changed

README.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ let ln = ISO_9899.Math.log(2.71828) // 1.0
116116
let log2 = ISO_9899.Math.log2(1024.0) // 10.0
117117

118118
// Absolute value and additional power functions
119-
let abs = ISO_9899.Math.fabs(-3.14) // 3.14
120-
let dist = ISO_9899.Math.hypot(3.0, 4.0) // 5.0
121-
let cubeRoot = ISO_9899.Math.cbrt(27.0) // 3.0
119+
let abs = ISO_9899.Math.fabs(-3.14) // 3.14
120+
let dist = ISO_9899.Math.hypot(3.0, 4.0) // 5.0
121+
let cubeRoot = ISO_9899.Math.cbrt(27.0) // 3.0
122+
let max = ISO_9899.Math.fmax(3.0, 5.0) // 5.0
123+
let min = ISO_9899.Math.fmin(3.0, 5.0) // 3.0
122124

123125
// Rounding functions
124126
let rounded = ISO_9899.Math.ceil(2.3) // 3.0
@@ -127,16 +129,20 @@ let nearest = ISO_9899.Math.round(2.5) // 3.0 (ties away from zero)
127129
let truncated = ISO_9899.Math.trunc(-2.7) // -2.0
128130

129131
// Classification functions
130-
let classification = ISO_9899.Math.fpclassify(1.0) // .normal
131-
let isFinite = ISO_9899.Math.isfinite(.infinity) // false
132-
let isNaN = ISO_9899.Math.isnan(.nan) // true
133-
let signBit = ISO_9899.Math.signbit(-0.0) // true
132+
let classification = ISO_9899.Math.fpclassify(1.0) // .normal
133+
let isFinite = ISO_9899.Math.isfinite(.infinity) // false
134+
let isNaN = ISO_9899.Math.isnan(.nan) // true
135+
let signBit = ISO_9899.Math.signbit(-0.0) // true
134136

135137
// Comparison functions (quiet - no exception on NaN)
136-
let greater = ISO_9899.Math.isgreater(5.0, 3.0) // true
137-
let lessOrGreater = ISO_9899.Math.islessgreater(.nan, 5.0) // false
138+
let greater = ISO_9899.Math.isgreater(5.0, 3.0) // true
139+
let lessOrGreater = ISO_9899.Math.islessgreater(5.0, .nan) // false
138140
let unordered = ISO_9899.Math.isunordered(.nan, 5.0) // true
139141

142+
// Integer-returning rounding
143+
let asInt = ISO_9899.Math.lround(2.5) // 3
144+
let asInt64 = ISO_9899.Math.llround(2.5) // 3
145+
140146
// Mathematical constants
141147
let circumference = 2 * ISO_9899.Math.Constants.pi * radius
142148
let radians = degrees * ISO_9899.Math.Constants.degreesToRadians
@@ -169,9 +175,11 @@ let log = x.c.log // 0.693147180...
169175
let log2 = x.c.log2 // 1.0
170176

171177
// Absolute value and additional power functions
172-
let abs = (-x).c.fabs // 2.0
178+
let abs = (-x).c.abs // 2.0
173179
let dist = x.c.hypot(3.0) // 3.60555127...
174180
let cube = x.c.cbrt // 1.25992104...
181+
let maximum = x.c.max(5.0) // 5.0
182+
let minimum = x.c.min(5.0) // 2.0
175183

176184
// Rounding functions
177185
let ceil = (2.3).c.ceil // 3.0
@@ -180,15 +188,19 @@ let round = (2.5).c.round // 3.0
180188
let trunc = (-2.7).c.trunc // -2.0
181189

182190
// Classification functions
183-
let classification = (1.0).c.fpclassify // .normal
184-
let isFinite = Double.infinity.c.isfinite // false
185-
let isNaN = Double.nan.c.isnan // true
186-
let signBit = (-0.0).c.signbit // true
191+
let classification = (1.0).c.classification // .normal
192+
let isFinite = Double.infinity.c.isFinite // false
193+
let isNaN = Double.nan.c.isNaN // true
194+
let hasNegativeSign = (-0.0).c.hasNegativeSign // true
187195

188196
// Comparison functions (quiet - no exception on NaN)
189-
let greater = (5.0).c.isgreater(3.0) // true
190-
let lessOrGreater = Double.nan.c.islessgreater(5.0) // false
191-
let unordered = Double.nan.c.isunordered(5.0) // true
197+
let greater = (5.0).c.isGreater(than: 3.0) // true
198+
let lessOrGreater = (5.0).c.isNotEqual(to: .nan) // false
199+
let unordered = Double.nan.c.isUnordered(with: 5.0) // true
200+
201+
// Integer-returning rounding
202+
let asInt = (2.5).c.roundToInt() // 3
203+
let asInt64 = (2.5).c.roundToInt64() // 3
192204

193205
// Works with Float too
194206
let y: Float = 1.5

Sources/ISO 9899/Double+ISO_9899.swift

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Double {
1919
/// let root = x.c.sqrt // 1.41421356...
2020
/// let sine = x.c.sin // 0.90929742...
2121
/// let log = x.c.log // 0.693147180...
22-
/// let abs = (-x).c.fabs // 2.0
22+
/// let abs = (-x).c.abs // 2.0
2323
/// ```
2424
///
2525
/// ## See Also
@@ -185,7 +185,7 @@ extension Double.C {
185185
///
186186
/// Delegates to ``ISO_9899/Math/fabs(_:)-1w0zq``
187187
@_transparent
188-
public var fabs: Double {
188+
public var abs: Double {
189189
ISO_9899.Math.fabs(self.value)
190190
}
191191

@@ -369,35 +369,43 @@ extension Double.C {
369369
ISO_9899.Math.nearbyint(self.value)
370370
}
371371

372-
/// Round to nearest integer and return as Int
372+
/// Round to nearest integer using current rounding mode
373+
///
374+
/// Returns the rounded value as an `Int`, using the current FPU rounding direction.
373375
///
374376
/// Delegates to ``ISO_9899/Math/lrint(_:)-7abaz``
375377
@_transparent
376-
public var lrint: Int {
378+
public func roundedToInt() -> Int {
377379
ISO_9899.Math.lrint(self.value)
378380
}
379381

380-
/// Round to nearest integer and return as Int64
382+
/// Round to nearest integer using current rounding mode
383+
///
384+
/// Returns the rounded value as an `Int64`, using the current FPU rounding direction.
381385
///
382386
/// Delegates to ``ISO_9899/Math/llrint(_:)-61a0k``
383387
@_transparent
384-
public var llrint: Int64 {
388+
public func roundedToInt64() -> Int64 {
385389
ISO_9899.Math.llrint(self.value)
386390
}
387391

388-
/// Round (ties away from zero) and return as Int
392+
/// Round to nearest integer (ties away from zero)
393+
///
394+
/// Returns the rounded value as an `Int`, with ties rounded away from zero.
389395
///
390396
/// Delegates to ``ISO_9899/Math/lround(_:)-4gv6i``
391397
@_transparent
392-
public var lround: Int {
398+
public func roundToInt() -> Int {
393399
ISO_9899.Math.lround(self.value)
394400
}
395401

396-
/// Round (ties away from zero) and return as Int64
402+
/// Round to nearest integer (ties away from zero)
403+
///
404+
/// Returns the rounded value as an `Int64`, with ties rounded away from zero.
397405
///
398406
/// Delegates to ``ISO_9899/Math/llround(_:)-4j6vs``
399407
@_transparent
400-
public var llround: Int64 {
408+
public func roundToInt64() -> Int64 {
401409
ISO_9899.Math.llround(self.value)
402410
}
403411
}
@@ -409,7 +417,7 @@ extension Double.C {
409417
///
410418
/// Delegates to ``ISO_9899/Math/fmod(_:_:)-47htk``
411419
@_transparent
412-
public func fmod(_ y: Double) -> Double {
420+
public func mod(_ y: Double) -> Double {
413421
ISO_9899.Math.fmod(self.value, y)
414422
}
415423

@@ -433,28 +441,30 @@ extension Double.C {
433441
// MARK: - Manipulation Functions (Section 7.12.11)
434442

435443
extension Double.C {
436-
/// Copy sign from another value
444+
/// Returns self with the sign of another value
445+
///
446+
/// Returns a value with the magnitude of self and the sign of `other`.
437447
///
438448
/// Delegates to ``ISO_9899/Math/copysign(_:_:)-76te9``
439449
@_transparent
440-
public func copysign(_ y: Double) -> Double {
441-
ISO_9899.Math.copysign(self.value, y)
450+
public func withSign(of other: Double) -> Double {
451+
ISO_9899.Math.copysign(self.value, other)
442452
}
443453

444-
/// Get next representable value toward y
454+
/// Returns the next representable value in the direction of another value
445455
///
446456
/// Delegates to ``ISO_9899/Math/nextafter(_:_:)-4hj4j``
447457
@_transparent
448-
public func nextafter(_ y: Double) -> Double {
449-
ISO_9899.Math.nextafter(self.value, y)
458+
public func nextRepresentable(toward other: Double) -> Double {
459+
ISO_9899.Math.nextafter(self.value, other)
450460
}
451461

452-
/// Get next representable value toward y (long double)
462+
/// Returns the next representable value in the direction of another value (extended precision)
453463
///
454464
/// Delegates to ``ISO_9899/Math/nexttoward(_:_:)-71rr4``
455465
@_transparent
456-
public func nexttoward(_ y: Double) -> Double {
457-
ISO_9899.Math.nexttoward(self.value, y)
466+
public func nextRepresentableExtended(toward other: Double) -> Double {
467+
ISO_9899.Math.nexttoward(self.value, other)
458468
}
459469
}
460470

@@ -463,102 +473,127 @@ extension Double.C {
463473
extension Double.C {
464474
/// Classify floating-point value
465475
///
476+
/// Returns the classification of the value (normal, zero, subnormal, infinite, or NaN).
477+
///
466478
/// Delegates to ``ISO_9899/Math/fpclassify(_:)-76te9``
467479
@_transparent
468-
public var fpclassify: ISO_9899.Math.FloatingPointClass {
480+
public var classification: ISO_9899.Math.FloatingPointClass {
469481
ISO_9899.Math.fpclassify(self.value)
470482
}
471483

472484
/// Test for finite value
473485
///
486+
/// Returns `true` if the value is finite (not infinite or NaN).
487+
///
474488
/// Delegates to ``ISO_9899/Math/isfinite(_:)-76te9``
475489
@_transparent
476-
public var isfinite: Bool {
490+
public var isFinite: Bool {
477491
ISO_9899.Math.isfinite(self.value)
478492
}
479493

480494
/// Test for infinity
481495
///
496+
/// Returns `true` if the value is positive or negative infinity.
497+
///
482498
/// Delegates to ``ISO_9899/Math/isinf(_:)-76te9``
483499
@_transparent
484-
public var isinf: Bool {
500+
public var isInfinite: Bool {
485501
ISO_9899.Math.isinf(self.value)
486502
}
487503

488504
/// Test for NaN
489505
///
506+
/// Returns `true` if the value is Not-a-Number (NaN).
507+
///
490508
/// Delegates to ``ISO_9899/Math/isnan(_:)-76te9``
491509
@_transparent
492-
public var isnan: Bool {
510+
public var isNaN: Bool {
493511
ISO_9899.Math.isnan(self.value)
494512
}
495513

496514
/// Test for normal value
497515
///
516+
/// Returns `true` if the value is normal (not zero, subnormal, infinite, or NaN).
517+
///
498518
/// Delegates to ``ISO_9899/Math/isnormal(_:)-76te9``
499519
@_transparent
500-
public var isnormal: Bool {
520+
public var isNormal: Bool {
501521
ISO_9899.Math.isnormal(self.value)
502522
}
503523

504-
/// Test sign bit
524+
/// Test if sign bit is set
525+
///
526+
/// Returns `true` if the sign bit is set (negative), including for -0.0 and -NaN.
505527
///
506528
/// Delegates to ``ISO_9899/Math/signbit(_:)-76te9``
507529
@_transparent
508-
public var signbit: Bool {
530+
public var hasNegativeSign: Bool {
509531
ISO_9899.Math.signbit(self.value)
510532
}
511533
}
512534

513535
// MARK: - Comparison Macros (Section 7.12.14)
514536

515537
extension Double.C {
516-
/// Determine whether value > y (quiet comparison)
538+
/// Determine whether self > other (quiet comparison)
539+
///
540+
/// Unlike the `>` operator, this does not raise an exception when comparing with NaN.
517541
///
518542
/// Delegates to ``ISO_9899/Math/isgreater(_:_:)-76te9``
519543
@_transparent
520-
public func isgreater(_ y: Double) -> Bool {
521-
ISO_9899.Math.isgreater(self.value, y)
544+
public func isGreater(than other: Double) -> Bool {
545+
ISO_9899.Math.isgreater(self.value, other)
522546
}
523547

524-
/// Determine whether value >= y (quiet comparison)
548+
/// Determine whether self >= other (quiet comparison)
549+
///
550+
/// Unlike the `>=` operator, this does not raise an exception when comparing with NaN.
525551
///
526552
/// Delegates to ``ISO_9899/Math/isgreaterequal(_:_:)-76te9``
527553
@_transparent
528-
public func isgreaterequal(_ y: Double) -> Bool {
529-
ISO_9899.Math.isgreaterequal(self.value, y)
554+
public func isGreaterOrEqual(to other: Double) -> Bool {
555+
ISO_9899.Math.isgreaterequal(self.value, other)
530556
}
531557

532-
/// Determine whether value < y (quiet comparison)
558+
/// Determine whether self < other (quiet comparison)
559+
///
560+
/// Unlike the `<` operator, this does not raise an exception when comparing with NaN.
533561
///
534562
/// Delegates to ``ISO_9899/Math/isless(_:_:)-76te9``
535563
@_transparent
536-
public func isless(_ y: Double) -> Bool {
537-
ISO_9899.Math.isless(self.value, y)
564+
public func isLess(than other: Double) -> Bool {
565+
ISO_9899.Math.isless(self.value, other)
538566
}
539567

540-
/// Determine whether value <= y (quiet comparison)
568+
/// Determine whether self <= other (quiet comparison)
569+
///
570+
/// Unlike the `<=` operator, this does not raise an exception when comparing with NaN.
541571
///
542572
/// Delegates to ``ISO_9899/Math/islessequal(_:_:)-76te9``
543573
@_transparent
544-
public func islessequal(_ y: Double) -> Bool {
545-
ISO_9899.Math.islessequal(self.value, y)
574+
public func isLessOrEqual(to other: Double) -> Bool {
575+
ISO_9899.Math.islessequal(self.value, other)
546576
}
547577

548-
/// Determine whether value < y or value > y (quiet comparison)
578+
/// Determine whether self != other (quiet comparison)
579+
///
580+
/// Returns `true` if self < other or self > other (excludes equal and unordered).
581+
/// Unlike `!=`, this does not raise an exception when comparing with NaN.
549582
///
550583
/// Delegates to ``ISO_9899/Math/islessgreater(_:_:)-76te9``
551584
@_transparent
552-
public func islessgreater(_ y: Double) -> Bool {
553-
ISO_9899.Math.islessgreater(self.value, y)
585+
public func isNotEqual(to other: Double) -> Bool {
586+
ISO_9899.Math.islessgreater(self.value, other)
554587
}
555588

556589
/// Determine whether arguments are unordered
557590
///
591+
/// Returns `true` if either self or other is NaN.
592+
///
558593
/// Delegates to ``ISO_9899/Math/isunordered(_:_:)-76te9``
559594
@_transparent
560-
public func isunordered(_ y: Double) -> Bool {
561-
ISO_9899.Math.isunordered(self.value, y)
595+
public func isUnordered(with other: Double) -> Bool {
596+
ISO_9899.Math.isunordered(self.value, other)
562597
}
563598
}
564599

@@ -567,25 +602,27 @@ extension Double.C {
567602
extension Double.C {
568603
/// Compute positive difference
569604
///
605+
/// Returns `self - y` if `self > y`, otherwise `+0`.
606+
///
570607
/// Delegates to ``ISO_9899/Math/fdim(_:_:)-2rksv``
571608
@_transparent
572-
public func fdim(_ y: Double) -> Double {
609+
public func positiveDifference(from y: Double) -> Double {
573610
ISO_9899.Math.fdim(self.value, y)
574611
}
575612

576613
/// Determine maximum value
577614
///
578615
/// Delegates to ``ISO_9899/Math/fmax(_:_:)-8ks7i``
579616
@_transparent
580-
public func fmax(_ y: Double) -> Double {
617+
public func max(_ y: Double) -> Double {
581618
ISO_9899.Math.fmax(self.value, y)
582619
}
583620

584621
/// Determine minimum value
585622
///
586623
/// Delegates to ``ISO_9899/Math/fmin(_:_:)-9yw9t``
587624
@_transparent
588-
public func fmin(_ y: Double) -> Double {
625+
public func min(_ y: Double) -> Double {
589626
ISO_9899.Math.fmin(self.value, y)
590627
}
591628

0 commit comments

Comments
 (0)