Velocity Reviews > how to convert double to short ?

how to convert double to short ?

Robert Sabocanec
Guest
Posts: n/a

 10-20-2003
"Alan" <(E-Mail Removed)> wrote in message
news:bmqs76\$1mri\$(E-Mail Removed)...
> how to convert double to short ?
> for example, I want to convert
>
> double doubleVal1 = 15000.1;
> double doubleVal2 = 12000.0;
> short shortVal;
>
> shortVal = doubleVal1 - doubleVal2;
>
> I want the result of shortVal = 3000 and store as short instead of

double
> value 3000.1
> how can I do that?
>

You could try numeric_cast from boost library, it's simple for use,
http://www.boost.org/libs/conversion...m#numeric_cast

Robi

Dan Pop
Guest
Posts: n/a

 10-20-2003
In <(E-Mail Removed)> Mark Gordon <(E-Mail Removed)> writes:

>On Sat, 18 Oct 2003 12:59:35 +0200
>
>> Mark Gordon wrote:
>>
>> >>how to convert double to short ?
>> >>[snip]

>>
>> > If you don't care about the direction of rounding you've just done
>> > it.

>>
>> For C99 at least the rounding direction of implicit conversions and
>> casts from floating-point types to integer types is defined as
>> towards-zero, i.e. truncating (6.3.1.4 clause 1). I don't have a C89
>> standard handy, but I suspect the same is true there.

>
>I thought than in C90 it was implementation defined.

Nope. C99 is no different from C89 on this particular issue.

>Based on what I
>have to hand I believe that float.h provides you with FLT_ROUNDS which
>gives you information about the rounding used.

It doesn't apply here.

The rounding mode for floating-point addition is characterized by the
^^^^^^^^^^^^^^^^^^^^^^^^^^^
value of FLT_ROUNDS : -1 indeterminable, 0 toward zero, 1 to nearest, 2
toward positive infinity, 3 toward negative infinity. All other values
for FLT_ROUNDS characterize implementation-defined rounding behavior.

The text relevant to this discussion is:

3.2.1.3 Floating and integral

When a value of floating type is converted to integral type, the
fractional part is discarded. If the value of the integral part
cannot be represented by the integral type, the behavior is
undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)

Alan Balmer
Guest
Posts: n/a

 10-20-2003
On Sat, 18 Oct 2003 15:46:54 +0800, "Alan"
<(E-Mail Removed)> wrote:

>how to convert double to short ?

With fear and trepidation. Plus range checking.

>for example, I want to convert
>
>double doubleVal1 = 15000.1;
>double doubleVal2 = 12000.0;
>short shortVal;
>
>shortVal = doubleVal1 - doubleVal2;
>
>I want the result of shortVal = 3000 and store as short instead of double
>value 3000.1
>how can I do that?
>
>

--
Al Balmer
Balmer Consulting
(E-Mail Removed)