Float to Uint

so i want to transform a float variable to Uint16 or Sint16

By either truncating it (throwing away everything after the decimal point) or
rounding it to the nearest integer. You didn’t say what library you’re working
in, but there should be standard functions for it, probably called Trunc and
Round.________________________________
From: dundunj@hotmail.com (VnzlaDun)
Subject: [SDL] float to Uint

so i want to transform a float variable to Uint16 or Sint16

so i want to transform a float variable to Uint16 or Sint16

float f;
Uint16 u;
Sint16 s;

u = f;
s = f;

Would that do?

ZoltanOn Sat, 9 Oct 2010, VnzlaDun wrote:

Yes, that works. Depending on your compiler you may need to cast them to
shut up the warnings.

float f;
Uint16 u;
Sint16 s;

u = (Uint16)f;
s = (Sint16)f;> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of zoltan at bendor.com.au
Sent: Sunday, October 10, 2010 7:57 PM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] float to Uint

On Sat, 9 Oct 2010, VnzlaDun wrote:

so i want to transform a float variable to Uint16 or Sint16

float f;
Uint16 u;
Sint16 s;

u = f;
s = f;

Would that do?

Zoltan


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

u = (Uint16) f;
s = (Sint16) s;

Without casting, your compiler should yell at you.

(Old habits: I never let code ship if there was even a single Warning message
from the compiler. This forced the programmers to make sure they really,
really knew what they were doing.)

JeffOn Sunday 10 October 2010 17:57, zoltan at bendor.com.au wrote:

On Sat, 9 Oct 2010, VnzlaDun wrote:

so i want to transform a float variable to Uint16 or Sint16

float f;
Uint16 u;
Sint16 s;

u = f;
s = f;

Would that do?

Zoltan

Depending on exactly what you’re doing, there is a subtle gotcha when
doing this kind of thing. This provides a pretty good explanation:
http://xania.org/200712/cpp-strict-aliasing

It won’t actually hurt you here because you’re not doing a reinterpret
style cast though. Just thought I’d mention it :).On 11/10/2010 02:50, Ken Rogoway wrote:

Yes, that works. Depending on your compiler you may need to cast them to
shut up the warnings.

float f;
Uint16 u;
Sint16 s;

 u = (Uint16)f;
 s = (Sint16)f;

If you read the article you linked you will see it has no relation to
the code you quoted.Em 11-10-2010 04:07, Tim Angus escreveu:

Depending on exactly what you’re doing, there is a subtle gotcha when
doing this kind of thing. This provides a pretty good explanation:
http://xania.org/200712/cpp-strict-aliasing


Daniel K. O.
“The only way to succeed is to build success yourself.”

Yes, I pointed this out in my reply.On 12/10/2010 04:08, Daniel K. O. wrote:

If you read the article you linked you will see it has no relation to
the code you quoted.