Skip to content

Commit

Permalink
Fixed some empty array bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperjeff committed Aug 28, 2022
1 parent 6b72da6 commit 11794fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/Physical/Core/Physical.swift
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ public struct Physical: CustomStringConvertible, Equatable, Hashable, Collection
public static func (left: Physical, right: Physical) -> Bool {
if left.value.isZero || right.value.isZero { return true }

if left.value.isNaN || right.value.isNaN { return false }

if left.values != nil || right.values != nil { return false }

let lcs = Swift.min(left.sigfigs, right.sigfigs)

func valueSignature(_ p: Physical) -> Int {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Physical/Experimental/PhysicalArrays.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension Array where Element == Double {

public extension Array where Element == Double {
var sigfigs: Int {
self.map { $0.sigfigs }.max()!
self.map { $0.sigfigs }.max() ?? 0
}
}

Expand Down
5 changes: 5 additions & 0 deletions Tests/PhysicalTests/PhysicalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ final class PhysicalTests: XCTestCase {
XCTAssert(((1.kg + 1.s) / 1.m).isNotAThing)
}

func testEmptyArrays() {
XCTAssert([].feet.sigfigs == 0)
XCTAssert([].feet.mean.value.isNaN)
}

func testBasicVectorOfValues() {
let a = Physical.Constants.Earth.surfaceGravity
let m = [12.37, 56.78, 90.12].kg
Expand Down

0 comments on commit 11794fd

Please sign in to comment.