On Sunday, January 7, 2018 at 6:09:45 AM UTC-5, Jan Mercl wrote: > > On Sun, Jan 7, 2018 at 11:57 AM T L <tapi...@gmail.com <javascript:>> > wrote: > > Yes. The choice is to raise a runtime error or not. When not, the value > does not really matter because every value of an integer variable > represents an integer number and there is no combination of the bits left > unused for NaN, Inf, etc. FTR, the value is what the CPU produces. The CPU > sets flags on the conversion but those are not available directly to the > program. The comma-ok idiom could be useful in such cases, though. >
There is not currently a comma-ok form for integer type conversions. The proposal in https://golang.org/issue/19624 would add one. Non-representable floating point values have to be checked before the > conversion where appropriate. > The spec <https://tip.golang.org/ref/spec#Conversions> says: “In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.” So you can check the value after the conversion, too, and that check is a bit simpler than trying to cover all of the cases beforehand: y := int64(x) if float64(y) != x { … } > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.