Skip to content

Commit 442021c

Browse files
committed
Fixed convertToFundamentalUnits to handle arrays
1 parent 9381628 commit 442021c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Sources/Physical/Core/Physical.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ public struct Physical: CustomStringConvertible, Equatable, Hashable, Collection
648648

649649
public mutating func convertToFundamentalUnits(unitsToConvert: [Dimension] = []) {
650650
var newValue = value
651+
var newValues = values
651652
var newUnits: DimensionDictionary = [:]
652653

653654
let hasNonZeroOffsets = units.containsNonZeroOffsetUnits()
@@ -674,6 +675,8 @@ public struct Physical: CustomStringConvertible, Equatable, Hashable, Collection
674675
// and milesPerImperialGallon & milesPerGallon use same units!
675676

676677
if hasNonZeroOffsets {
678+
// FIXME: add array version
679+
677680
newValue = Measurement(value: value, unit: unit).converted(to: base).value
678681
}
679682
else {
@@ -684,7 +687,16 @@ public struct Physical: CustomStringConvertible, Equatable, Hashable, Collection
684687
unitValue = 12
685688
}
686689

687-
newValue *= pow(Measurement(value: unitValue, unit: unit).converted(to: base).value, Double(exponent.realValue))
690+
let scaling = pow(Measurement(value: unitValue, unit: unit).converted(to: base).value, Double(exponent.realValue))
691+
692+
if let values = values {
693+
var result = [Double](repeating: 0, count: values.count)
694+
cblas_daxpy(Int32(values.count), scaling, values, 1, &result, 1)
695+
696+
newValues = result
697+
}
698+
699+
newValue *= scaling
688700
}
689701

690702
if newUnits.keys.contains(base),
@@ -700,6 +712,7 @@ public struct Physical: CustomStringConvertible, Equatable, Hashable, Collection
700712
if !newUnits.isEmpty {
701713
units = newUnits
702714
value = newValue
715+
values = newValues
703716
}
704717
}
705718

Tests/PhysicalTests/PhysicalTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ final class PhysicalTests: XCTestCase {
195195
print(acos([1, 0.993, 0.971, 0.935, 0.885, 0.823, 0.749, 0.663, 0.568, 0.465, 0.355, 0.239, 0.121, 0, -0.121, -0.239, -0.355, -0.465, -0.568, -0.663, -0.749, -0.823, -0.885, -0.935, -0.971, -0.993, -1].constant.sigfigs(3)))
196196
}
197197

198+
func testByteArrays() {
199+
let fileSizes = [1, 3, 4, -2].gigabytes.sigfigs(3)
200+
let dataRate = 1.megabits.perSecond.sigfigs(3)
201+
202+
let times = fileSizes / dataRate .minutes
203+
204+
XCTAssert(times[0] == 133.3.minutes) // why 4 sigfigs here?
205+
XCTAssert(times[2] == 533.3.minutes)
206+
}
207+
198208
/*func testPowers() {
199209
let a = [12.1, 3.6, -1.2, 0].miles
200210

0 commit comments

Comments
 (0)