Skip to content

Commit

Permalink
Double precision is more precise
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Jun 16, 2017
1 parent a239236 commit 46cf1e8
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public struct JSON {
mutating func parseNumber() throws -> Value {
var negate = false

func parseInteger(autoNegate: Bool = false) -> String? {
func parseInteger(autoNegate: Bool = false) -> Int? {
var int = (negate && autoNegate) ? "-" : ""

// Loop over all digits
Expand All @@ -209,14 +209,14 @@ public struct JSON {
position += 1
}

return int
return Int(int)
}

func parseExp() -> Int? {
if data[position] == SpecialCharacters.minus {
position += 1

if let number = Int(parseInteger()) {
if let number = parseInteger() {
return -number
}

Expand All @@ -226,7 +226,7 @@ public struct JSON {
position += 1
}

return Int(parseInteger())
return parseInteger()
}
}

Expand All @@ -250,11 +250,11 @@ public struct JSON {
throw JSONError.invalidNumber
}

guard let parsedBaseNumber = Double("\(int).\(fracture)") else {
guard let parsedFracture = Double("0.\(fracture)") else {
throw JSONError.invalidNumber
}

let baseNumber = negate ? -parsedBaseNumber : parsedBaseNumber
let baseNumber = Double(int) + (negate ? -parsedFracture : parsedFracture)

if remaining(1), data[position] == 0x45 || data[position] == 0x65 {
position += 1
Expand All @@ -268,13 +268,13 @@ public struct JSON {
return baseNumber

// `E` or `e`
} else if data[position] == 0x45 || data[position] == 0x65, let int = Double(int) {
} else if data[position] == 0x45 || data[position] == 0x65 {
position += 1
guard let exp = parseExp() else {
throw JSONError.invalidNumber
}

return int * pow(10, Double(exp))
return Double(int) * pow(10, Double(exp))
} else {
return int
}
Expand Down

0 comments on commit 46cf1e8

Please sign in to comment.