| | 8 | |
|---|
| | 9 | yaml_as "tag:yaml.org,2002:float" |
|---|
| | 10 | def to_yaml( opts = {} ) |
|---|
| | 11 | YAML::quick_emit( nil, opts ) do |out| |
|---|
| | 12 | # This emits the number without any scientific notation. |
|---|
| | 13 | # I prefer it to using self.to_f.to_s, which would lose precision. |
|---|
| | 14 | # |
|---|
| | 15 | # Note that YAML allows that when reconsituting floats |
|---|
| | 16 | # to native types, some precision may get lost. |
|---|
| | 17 | # There is no full precision real YAML tag that I am aware of. |
|---|
| | 18 | str = self.to_s |
|---|
| | 19 | if str == "Infinity" |
|---|
| | 20 | str = ".Inf" |
|---|
| | 21 | elsif str == "-Infinity" |
|---|
| | 22 | str = "-.Inf" |
|---|
| | 23 | elsif str == "NaN" |
|---|
| | 24 | str = ".NaN" |
|---|
| | 25 | end |
|---|
| | 26 | out.scalar( "tag:yaml.org,2002:float", str, :plain ) |
|---|
| | 27 | end |
|---|
| | 28 | end |
|---|