Skip to content

Commit

Permalink
HugeFloat now conforms to Coddle
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomHashTags committed Apr 23, 2023
1 parent fd06a1a commit 90b4369
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/huge-numbers/HugeFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

// TODO: expand functionality
/// Default unit is in degrees, or no unit at all (just a raw number).
public struct HugeFloat : Hashable, Comparable {
public struct HugeFloat : Hashable, Comparable, Codable {

public static var zero:HugeFloat = HugeFloat(integer: HugeInt.zero)
public static var one:HugeFloat = HugeFloat(integer: HugeInt.one)
Expand Down Expand Up @@ -111,6 +111,16 @@ public struct HugeFloat : Hashable, Comparable {
self.init(String(describing: integer))
}

public init(from decoder: Decoder) throws {
let container:SingleValueDecodingContainer = try decoder.singleValueContainer()
let string:String = try container.decode(String.self)
self.init(string)
}
public func encode(to encoder: Encoder) throws {
var container:SingleValueEncodingContainer = encoder.singleValueContainer()
try container.encode(description)
}

public var represented_float : Float {
return Float(description) ?? 0
}
Expand Down

0 comments on commit 90b4369

Please sign in to comment.