digitalmars.D - Can we drop cast from float to int ?
- #ponce (5/5) Mar 22 2010 I never knew exactly what the float-to-int C cast is supposed to do.
- Trass3r (2/6) Mar 22 2010 I've never heard anything else but truncate. Rounding can thus be done
- Lionello Lunesu (2/12) Mar 22 2010 or -0.5f when f < 0.0
- Jesse Phillips (3/8) Mar 22 2010 I've never considered anything but truncate.
- #ponce (2/6) Mar 23 2010 Well, ok it's not. It's just that it seems to me that it's useless and c...
- Jonathan M Davis (8/16) Mar 23 2010 Maybe I've just never talked to enough programmers about it, but I've ne...
I never knew exactly what the float-to-int C cast is supposed to do. float f = whatever(); int b = cast(int) f; Does it truncate, floor or round ? Or even undefined, depending on the rounding mode ? I just asked to 3 fellow programmers, it seems like i'm not the onlyone to find this confusing. Why should a destructive cast should be _that_ implicit anyway ?
Mar 22 2010
I never knew exactly what the float-to-int C cast is supposed to do. float f = whatever(); int b = cast(int) f; Does it truncate, floor or round ?I've never heard anything else but truncate. Rounding can thus be done with cast(int)(f + 0.5f).
Mar 22 2010
On 23-3-2010 7:59, Trass3r wrote:or -0.5f when f < 0.0I never knew exactly what the float-to-int C cast is supposed to do. float f = whatever(); int b = cast(int) f; Does it truncate, floor or round ?I've never heard anything else but truncate. Rounding can thus be done with cast(int)(f + 0.5f).
Mar 22 2010
#ponce wrote:I never knew exactly what the float-to-int C cast is supposed to do. float f = whatever(); int b = cast(int) f; Does it truncate, floor or round ? Or even undefined, depending on the rounding mode ? I just asked to 3 fellow programmers, it seems like i'm not the onlyone to find this confusing. Why should a destructive cast should be _that_ implicit anyway ?I've never considered anything but truncate. Also how is it implicit?
Mar 22 2010
I've never considered anything but truncate. Also how is it implicit?Well, ok it's not. It's just that it seems to me that it's useless and confusing. If it's really trunc, I have seen countless errors where it was used as floor.
Mar 23 2010
#ponce wrote:Maybe I've just never talked to enough programmers about it, but I've never talked to one who thought that it was anything other than truncate. I would have expected it to be mentioned in a decent C or C++ primer, but I haven't read any in a while, so I don't recally whether it's typical for it to be mentioned there. However, it is quite standard for C-derived languages to truncate when casting from float to integer. - Jonathan M DavisI've never considered anything but truncate. Also how is it implicit?Well, ok it's not. It's just that it seems to me that it's useless and confusing. If it's really trunc, I have seen countless errors where it was used as floor.
Mar 23 2010