Skip to content

Commit 2bfd37f

Browse files
authored
Merge pull request #5226 from ImranQasim/master
Fix FloatingPoint functions: Replace instance calls with static Double
2 parents ab66734 + afc7c98 commit 2bfd37f

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

Source/Charts/Utils/ChartUtils.swift

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,105 +12,75 @@
1212
import Foundation
1313
import CoreGraphics
1414

15-
extension Comparable
16-
{
17-
func clamped(to range: ClosedRange<Self>) -> Self
18-
{
19-
if self > range.upperBound
20-
{
15+
extension Comparable {
16+
func clamped(to range: ClosedRange<Self>) -> Self {
17+
if self > range.upperBound {
2118
return range.upperBound
22-
}
23-
else if self < range.lowerBound
24-
{
19+
} else if self < range.lowerBound {
2520
return range.lowerBound
26-
}
27-
else
28-
{
21+
} else {
2922
return self
3023
}
3124
}
3225
}
3326

34-
extension FloatingPoint
35-
{
36-
var DEG2RAD: Self
37-
{
27+
extension FloatingPoint {
28+
var DEG2RAD: Self {
3829
return self * .pi / 180
3930
}
4031

41-
var RAD2DEG: Self
42-
{
32+
var RAD2DEG: Self {
4333
return self * 180 / .pi
4434
}
4535

46-
/// - Note: Value must be in degrees
47-
/// - Returns: An angle between 0.0 < 360.0 (not less than zero, less than 360)
48-
var normalizedAngle: Self
49-
{
36+
var normalizedAngle: Self {
5037
let angle = truncatingRemainder(dividingBy: 360)
5138
return (sign == .minus) ? angle + 360 : angle
5239
}
5340
}
5441

55-
extension CGSize
56-
{
57-
func rotatedBy(degrees: CGFloat) -> CGSize
58-
{
42+
extension CGSize {
43+
func rotatedBy(degrees: CGFloat) -> CGSize {
5944
let radians = degrees.DEG2RAD
6045
return rotatedBy(radians: radians)
6146
}
6247

63-
func rotatedBy(radians: CGFloat) -> CGSize
64-
{
48+
func rotatedBy(radians: CGFloat) -> CGSize {
6549
return CGSize(
6650
width: abs(width * cos(radians)) + abs(height * sin(radians)),
6751
height: abs(width * sin(radians)) + abs(height * cos(radians))
6852
)
6953
}
7054
}
7155

72-
extension Double
73-
{
74-
/// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway.
75-
func roundedToNextSignificant() -> Double
76-
{
77-
guard
78-
!isInfinite,
79-
!isNaN,
80-
self != 0
81-
else { return self }
56+
extension Double {
57+
func roundedToNextSignificant() -> Double {
58+
guard !isInfinite, !isNaN, self != 0 else { return self }
8259

83-
let d = ceil(log10(self < 0 ? -self : self))
60+
// Use Foundation.log10
61+
let d = ceil(Foundation.log10(self < 0 ? -self : self))
8462
let pw = 1 - Int(d)
85-
let magnitude = pow(10.0, Double(pw))
63+
64+
// Use Foundation.pow
65+
let magnitude = Foundation.pow(10.0, Double(pw))
8666
let shifted = (self * magnitude).rounded()
8767
return shifted / magnitude
8868
}
8969

90-
var decimalPlaces: Int
91-
{
92-
guard
93-
!isNaN,
94-
!isInfinite,
95-
self != 0.0
96-
else { return 0 }
70+
var decimalPlaces: Int {
71+
guard !isNaN, !isInfinite, self != 0.0 else { return 0 }
9772

9873
let i = roundedToNextSignificant()
9974

100-
guard
101-
!i.isInfinite,
102-
!i.isNaN
103-
else { return 0 }
75+
guard !i.isInfinite, !i.isNaN else { return 0 }
10476

105-
return Int(ceil(-log10(i))) + 2
77+
// Use Foundation.log10
78+
return Int(ceil(-Foundation.log10(i))) + 2
10679
}
10780
}
10881

109-
extension CGPoint
110-
{
111-
/// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center.
112-
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint
113-
{
82+
extension CGPoint {
83+
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint {
11484
return CGPoint(x: x + distance * cos(angle.DEG2RAD),
11585
y: y + distance * sin(angle.DEG2RAD))
11686
}

0 commit comments

Comments
 (0)