这是indexloc提供的服务,不要输入任何密码
Skip to content

Consider adding a compiler flag -vet-conversion to warn about implicit conversions that may result in loss of data, including converting integers to floats #5495

@bloated-complexity

Description

@bloated-complexity

MIRSA discourages implicit conversions outright between integral and floating-point types.
GCC has the -Wconversion flag that can be used to warn about implicit conversions that may result in loss of data, including converting integers to floats.

// compile time values and types with implicit conversions
main :: proc() {
    i :: 1000000160
    f: f32 : i
    fmt.printfln("% 20.1f", f) // 1000000128.0
    //                       i :: 1000000160
}
// with explicit typing constant and dynamic cast
main :: proc() {
    f: f64
    i: int : 9223372036854775807
    f = cast(f64)i
    fmt.printfln("% 20.1f", f) // 9223372036854775808.0
    //                  i : int : 9223372036854775807
}
// same as above but with a variable vs. constant for both
package main
import "core:fmt"
main :: proc() {
    f: f64
    i: int = 9223372036854775807
    f = cast(f64)i
    fmt.printfln("% 20.1f", f) // 9223372036854775808.0
    //                       i := 9223372036854775807
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions