Background
I am creating an array of Double values, hours, that are computed around a central value, price. For x iteration, percent is used to calculate values at that percentage above and then below the price. The list is then formatted as,
- Above values (+ percent for iteration)
- Central value
- Below values (- percent for iteration)
I realize the way I am doing this is kind of hacky and I suspect my issue lies within this code.
let price : Double = earliestExpiryLeg.object(forKey: "underlying") as! Double var i = 1.00 var x = price var a : Array<Double> = [] as Array while i <= self.iteration { let y = x * (1 + self.percent) a.append((round (y*100)/100)) x = y i = i + 1.00 } var w = 1.00 var s = price var r : Array<Double> = [] as Array while w <= self.iteration { let y = s * (1 - self.percent) r.append((round (y*100)/100)) s = y w = w + 1.00 } a.reverse() a.append(price) a.append(contentsOf: r) self.hours = a What I want to do
I now want to compare a value from hours to the original price value and get the percent difference. I am using the following function to accomplish this.
func pctDiff(x1: CGFloat, x2: CGFloat) -> Double { let diff = (x2 - x1) / x1 let doub = Double((100 * (diff * 100) / 100)) return doub } self.pctDiff(x1: CGFloat(earliestExpiryLeg.object(forKey: "underlying") as? Double ?? 0.00), x2: CGFloat(hours![indexPath.row - 2])) What's going wrong
The percent change values returned are not exact, even though percent is exact. For example, if percent is 10.00%, I would expect the percentage change for instance to be 30.00%-even for a value three iteration above the central price value, but instead I get 33.10%.
没有评论:
发表评论