-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replaced relevant ChartUtils
methods with Double
extensions
#2994
Changes from 6 commits
5dd1091
593e73c
23aa2f2
54440e1
7a34187
a9e72c1
6a57a6a
672f25e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,40 @@ extension CGSize { | |
} | ||
} | ||
|
||
extension Double { | ||
/// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway. | ||
func roundedToNextSignficant() -> Double { | ||
guard | ||
!isInfinite, | ||
!isNaN, | ||
self != 0 | ||
else { return self } | ||
|
||
let d = ceil(log10(self < 0 ? -self : self)) | ||
let pw = 1 - Int(d) | ||
let magnitude = pow(10.0, Double(pw)) | ||
let shifted = (self * magnitude).rounded() | ||
return shifted / magnitude | ||
} | ||
|
||
var decimalPlaces: Int { | ||
guard | ||
!isNaN, | ||
!isInfinite, | ||
self != 0.0 | ||
else { return 0 } | ||
|
||
let i = self.roundedToNextSignficant() | ||
|
||
guard | ||
!i.isInfinite, | ||
!i.isNaN | ||
else { return 0 } | ||
|
||
return Int(ceil(-log10(i))) + 2 | ||
} | ||
} | ||
|
||
open class ChartUtils | ||
{ | ||
fileprivate static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() | ||
|
@@ -41,50 +75,7 @@ open class ChartUtils | |
internal static let DEG2RAD = Double.pi / 180.0 | ||
internal static let RAD2DEG = 180.0 / Double.pi | ||
} | ||
|
||
internal class func roundToNextSignificant(number: Double) -> Double | ||
{ | ||
if number.isInfinite || number.isNaN || number == 0 | ||
{ | ||
return number | ||
} | ||
|
||
let d = ceil(log10(number < 0.0 ? -number : number)) | ||
let pw = 1 - Int(d) | ||
let magnitude = pow(Double(10.0), Double(pw)) | ||
let shifted = round(number * magnitude) | ||
return shifted / magnitude | ||
} | ||
|
||
internal class func decimals(_ number: Double) -> Int | ||
{ | ||
if number.isNaN || number.isInfinite || number == 0.0 | ||
{ | ||
return 0 | ||
} | ||
|
||
let i = roundToNextSignificant(number: Double(number)) | ||
|
||
if i.isInfinite || i.isNaN | ||
{ | ||
return 0 | ||
} | ||
|
||
return Int(ceil(-log10(i))) + 2 | ||
} | ||
|
||
internal class func nextUp(_ number: Double) -> Double | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swift doc:
I'm kind of confused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only place this is used is in the axis renderers. I removed this implementation because the default one is safer to pass into the call sites. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep but it's different than the library implementation, towards the infinity check and result. I need to check several places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but the real issue is swift doc has paradox:
does infinity here contains both +inf and -inf? or just +inf? because it added at last line:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
if number.isInfinite || number.isNaN | ||
{ | ||
return number | ||
} | ||
else | ||
{ | ||
return number + Double.ulpOfOne | ||
} | ||
} | ||
|
||
|
||
/// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center. | ||
internal class func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm no native speaker. is
decimalPlaces
ok? @petester42 @danielgindi