Skip to content
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

float64 not supported when marshalling to YAML #83

Closed
ghodss opened this issue Feb 17, 2015 · 1 comment
Closed

float64 not supported when marshalling to YAML #83

ghodss opened this issue Feb 17, 2015 · 1 comment

Comments

@ghodss
Copy link

ghodss commented Feb 17, 2015

Example code:

package main

import (
    "fmt"
    "math"

    "gopkg.in/v2/yaml"
)

type A struct {
    F float64
}

func main() {
    f32 := A{math.MaxFloat32}
    f64 := A{math.MaxFloat64}

    y32, err := yaml.Marshal(f32)
    if err != nil {
        fmt.Printf("%v\n", err)
    }

    y64, err := yaml.Marshal(f64)
    if err != nil {
        fmt.Printf("%v\n", err)
    }

    fmt.Printf("Should contain %v: %v", math.MaxFloat32, string(y32))
    fmt.Printf("Should contain %v: %v", math.MaxFloat64, string(y64))
}

// output:
Should contain 3.4028234663852886e+38: f: 3.4028235e+38
Should contain 1.7976931348623157e+308: f: .inf

Seems to be related to this line of code: https://github.com/go-yaml/yaml/blob/v2/encode.go#L285

@udhos
Copy link

udhos commented Mar 15, 2018

I have just tested this code and it seems to work correctly.

lab@ubu1:~$ more f.go
package main

import (
        "fmt"
        "math"

        "gopkg.in/v2/yaml"
)

type A struct {
        F float64
}

func main() {
        f32 := A{math.MaxFloat32}
        f64 := A{math.MaxFloat64}

        y32, err := yaml.Marshal(f32)
        if err != nil {
                fmt.Printf("%v\n", err)
        }

        y64, err := yaml.Marshal(f64)
        if err != nil {
                fmt.Printf("%v\n", err)
        }

        fmt.Printf("Should contain %v: %v", math.MaxFloat32, string(y32))
        fmt.Printf("Should contain %v: %v", math.MaxFloat64, string(y64))
}
lab@ubu1:~$ go run f.go
Should contain 3.4028234663852886e+38: f: 3.4028234663852886e+38
Should contain 1.7976931348623157e+308: f: 1.7976931348623157e+308
lab@ubu1:~$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants