Skip to content

Commit

Permalink
have swift-mode of emacs reindent sources
Browse files Browse the repository at this point in the history
  • Loading branch information
dankogai committed Nov 16, 2023
1 parent 6e73287 commit b2fe462
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
35 changes: 17 additions & 18 deletions Sources/BigNum/BigFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ extension BigFloat {
//
public var sign: FloatingPointSign {
return 0 < mantissa ? .plus
: mantissa < 0 ? .minus
: scale < 0 ? .minus : .plus // ±0 and inifinity
: mantissa < 0 ? .minus
: scale < 0 ? .minus : .plus // ±0 and inifinity
}
public mutating func negate() {
if scale != -Int.max-1 && Swift.abs(scale) == Int.max {
Expand Down Expand Up @@ -116,9 +116,9 @@ extension BinaryFloatingPoint {
#endif
let offset = Swift.max(bf.mantissa.bitWidth-1 - (Self.significandBitCount+1), 0)
self.init(
sign:bf.sign,
exponent:Exponent(bf.scale + offset),
significand:Self(F(bf.mantissa.magnitude >> offset))
sign:bf.sign,
exponent:Exponent(bf.scale + offset),
significand:Self(F(bf.mantissa.magnitude >> offset))
)
}
}
Expand All @@ -133,9 +133,9 @@ extension Double {
else {
let offset = Swift.max(bf.mantissa.bitWidth-1 - 64, 0)
self.init(
sign:bf.sign,
exponent:Exponent(bf.scale + offset),
significand:Double(bf.mantissa.magnitude >> offset)
sign:bf.sign,
exponent:Exponent(bf.scale + offset),
significand:Double(bf.mantissa.magnitude >> offset)
)
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ extension BigFloat : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral {
let w = max(q.num.bitWidth, q.den.bitWidth) - 1
let qt = BigInt(q.num).over(BigInt(q.den)).truncated(width:w+px, round:rule)
self = BigFloat(scale:-qt.den.trailingZeroBitCount, mantissa:qt.num)
}
}
}
}
extension BigFloat {
Expand All @@ -237,8 +237,8 @@ extension BigFloat {
}
public func isEqual(to other:BigFloat)->Bool {
return self.isNaN || other.isNaN ? false
: self.isZero ? other.isZero
: self.isIdentical(to: other)
: self.isZero ? other.isZero
: self.isIdentical(to: other)
}
public static func ==(_ lhs:BigFloat, _ rhs:BigFloat)->Bool {
return lhs.isEqual(to:rhs)
Expand Down Expand Up @@ -284,8 +284,8 @@ extension BigFloat : Comparable {
}
public func isTotallyOrdered(belowOrEqualTo other: BigFloat) -> Bool {
return self.isNaN ? other.isNaN
: self.isZero && other.isZero ? self.sign == .minus || other.sign == .plus
: self.isLessThanOrEqualTo(other)
: self.isZero && other.isZero ? self.sign == .minus || other.sign == .plus
: self.isLessThanOrEqualTo(other)
}
}
/// SignedNumeric!
Expand Down Expand Up @@ -353,22 +353,22 @@ extension BigFloat : FloatingPoint{
public func quotientAndRemainder(dividingBy other: BigFloat,
precision px:Int=precision,
round rule:FloatingPointRoundingRule=roundingRule)
-> (quotient:BigFloat, remainder:BigFloat) {
-> (quotient:BigFloat, remainder:BigFloat) {
let (q, r) = BigRat(self).quotientAndRemainder(dividingBy: BigRat(other))
return (quotient:BigFloat(q), remainder:BigFloat(r, precision:px, round:rule))
}
public func truncatingRemainder(dividingBy other: BigFloat,
precision px:Int=precision,
round rule:FloatingPointRoundingRule=roundingRule)->BigFloat
{
return self.quotientAndRemainder(dividingBy:other, precision:px, round:rule).quotient
return self.quotientAndRemainder(dividingBy:other, precision:px, round:rule).quotient
}
public mutating func formTruncatingRemainder(dividingBy other: BigFloat) {
self = self.quotientAndRemainder(dividingBy: other).quotient
}
public func remainder(dividingBy other: BigFloat,
precision px:Int=precision,
round rule:FloatingPointRoundingRule=roundingRule)->BigFloat
precision px:Int=precision,
round rule:FloatingPointRoundingRule=roundingRule)->BigFloat
{
return self.quotientAndRemainder(dividingBy:other, precision:px, round:rule).remainder
}
Expand Down Expand Up @@ -499,5 +499,4 @@ extension String {
init(_ bf:BigFloat, radix:Int=10, uppercase:Bool=false){
self = uppercase ? bf.toString(radix:radix).uppercased() : bf.toString(radix:radix)
}

}
3 changes: 1 addition & 2 deletions Sources/BigNum/BigNum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension SignedInteger {
case .down: i += r < 0 ? -1 : 0
case .up: i += 0 < r ? +1 : 0
case .towardZero: i += 0
@unknown default: fatalError()
@unknown default: fatalError()
}
self = i << t
}
Expand All @@ -127,4 +127,3 @@ extension SignedInteger {
return result
}
}

15 changes: 7 additions & 8 deletions Sources/BigNum/GenericMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ extension BigFloatingPoint {
}
else { // x.isZero
return ysgn * (
y.isZero ? (x.sign == .minus ? PI(precision:px) : 0) : PI(precision: px)/2
y.isZero ? (x.sign == .minus ? PI(precision:px) : 0) : PI(precision: px)/2
)
}
}
Expand All @@ -143,7 +143,7 @@ extension BigFloatingPoint {
}
return r.truncated(width:px)
}
/// x ** y
/// x ** y
public static func pow(_ x:Self, _ y:Self, precision px:Int=Self.precision, debug db:Bool=false)->Self {
if x.isNaN || x.isInfinite || x.isZero || y.isNaN || y.isInfinite || y.isZero {
return Self(Double.pow(x.asDouble, y.asDouble))
Expand Down Expand Up @@ -234,8 +234,8 @@ extension BigFloatingPoint {
if x.isLess(than:0) { return 1/exp2(-x, precision:px, debug:db) }
let (ix, fx) = x.asMixed
let (ir, fr) = (
Self(2.0).power(ix, precision:px),
exp(fx * LN2(precision:px, debug:db), precision:px, debug:db)
Self(2.0).power(ix, precision:px),
exp(fx * LN2(precision:px, debug:db), precision:px, debug:db)
)
let r = ir * fr
return 0 < px ? r : r.truncated(width:px)
Expand Down Expand Up @@ -433,8 +433,8 @@ extension BigFloatingPoint {
func inner(_ x:Self)->Self {
guard x < 0.5 else {
return x < 1
? atan1 - inner((1-x).divided(by:1+x, precision:px))
: 2*atan1 - inner(1/x)
? atan1 - inner((1-x).divided(by:1+x, precision:px))
: 2*atan1 - inner(1/x)
}
let x2 = x*x
let x2p1 = 1 + x2
Expand Down Expand Up @@ -486,8 +486,7 @@ extension BigFloatingPoint {
var r = ax < 1 ? inner(ax) : 2*atan1 - inner(1/x)
if 0 < px { r.truncate(width: px) }
return x.sign == .minus ? -r : +r
}

}
/// arccos
public static func acos(_ x:Self, precision px:Int=Self.precision, debug db:Bool=false)->Self {
if (x - 1).isZero || 1 < Swift.abs(x) {
Expand Down
28 changes: 14 additions & 14 deletions Sources/BigNum/Rational.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ extension RationalType {
public var ulp:Self { return Self.zero }
public var sign:FloatingPointSign {
return num != 0
? num < 0 ? .minus : .plus
: den < 0 ? .minus : .plus
? num < 0 ? .minus : .plus
: den < 0 ? .minus : .plus
}
/// decompose to sign, exponent and significand
/// - sign: .minus or .plus
Expand Down Expand Up @@ -141,8 +141,8 @@ extension RationalType {
}
public func isEqual(to other: Self) -> Bool {
return self.isNaN || other.isNaN ? false
: self.isZero ? other.isZero
: self.isIdentical(to: other)
: self.isZero ? other.isZero
: self.isIdentical(to: other)
}
private func isLessThan(_ other:Self, onEqual:Bool)->Bool {
if self.isEqual(to: other) { return onEqual }
Expand All @@ -165,8 +165,8 @@ extension RationalType {
}
public func isTotallyOrdered(belowOrEqualTo other: Self) -> Bool {
return self.isNaN ? other.isNaN
: self.isZero && other.isZero ? self.sign == .minus || other.sign == .plus
: self.isLessThanOrEqualTo(other)
: self.isZero && other.isZero ? self.sign == .minus || other.sign == .plus
: self.isLessThanOrEqualTo(other)
}
//
public mutating func addingProduct(_ lhs: Self, _ rhs: Self)->Self {
Expand Down Expand Up @@ -209,7 +209,7 @@ extension RationalType {
case .down: i += r < 0.0 ? -1 : 0
case .up: i += 0.0 < r ? +1 : 0
case .towardZero: i += 0
@unknown default: fatalError()
@unknown default: fatalError()
}
self = Self(i)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ extension RationalType {
if rhs.isNaN { return Self.nan }
if rhs.isZero {
return lhs.isInfinite ? Self.nan
: lhs.sign == rhs.sign ? Self.zero : Self.negativeZero
: lhs.sign == rhs.sign ? Self.zero : Self.negativeZero
}
return Self(lhs.num * rhs.num, lhs.den * rhs.den)
}
Expand All @@ -323,7 +323,7 @@ extension RationalType {
if q.isNaN { return Self.nan }
if q.isInfinite {
return self.isInfinite ? Self.nan
: self.sign == q.sign ? Self.zero : Self.negativeZero
: self.sign == q.sign ? Self.zero : Self.negativeZero
}
return Self(self.num * q.den, self.den * q.num)
}
Expand Down Expand Up @@ -382,9 +382,9 @@ extension RationalType {
if rhs.isInfinite {
return lhs.isZero || lhs.sign == rhs.sign ? rhs : Self.nan
}
return Self(
lhs.num * rhs.den + rhs.num * lhs.den,
lhs.den * rhs.den
return Self(
lhs.num * rhs.den + rhs.num * lhs.den,
lhs.den * rhs.den
)
}
public static func +(_ lhs:Self, _ rhs:Element)->Self {
Expand Down Expand Up @@ -544,7 +544,7 @@ extension FixedWidthRationalElement {
// FixedWidthRationalType is Codable
/// Rational number type whose numerator and denominator are `RationalElement`
public protocol FixedWidthRationalType : RationalType, CustomDebugStringConvertible,Codable
where Element: FixedWidthRationalElement {}
where Element: FixedWidthRationalElement {}

extension FixedWidthRationalType {
public static var max:Self {
Expand All @@ -568,7 +568,7 @@ extension FixedWidthRationalType {
}

public struct FixedWidthRational<I:FixedWidthRationalElement> :
FixedWidthRationalType, Codable
FixedWidthRationalType, Codable
{
public typealias IntegerLiteralType = Int
public typealias FloatLiteralType = Double
Expand Down

0 comments on commit b2fe462

Please sign in to comment.