assignmnet to int form float/double | | |
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?
Are there caveeats/pits?
(Apart from the range and the precision of course)
What would be the best way to do it?
thanks,
marc | | | | re: assignmnet to int form float/double
Marc Schellens wrote:
[color=blue]
> My compiler warns about assignment to int form
> float/double.
> Will it nevertheless do what I expect?[/color]
Depends on what you expect.
[color=blue]
> Are there caveeats/pits?
> (Apart from the range and the precision of course)[/color]
Again, that depends on what you expect.
[color=blue]
> What would be the best way to do it?[/color]
If you want to remove the warning, do a cast:
int main()
{
double d(3); int i;
i = static_cast<int>(d);
} | | | | re: assignmnet to int form float/double
Rolf Magnus wrote:[color=blue]
> Marc Schellens wrote:
>
>[color=green]
>>My compiler warns about assignment to int form
>>float/double.
>>Will it nevertheless do what I expect?[/color]
>
>
> Depends on what you expect.[/color]
Come on, thats obvious:
I want an integer value close to the float value (if its in the range).
If its out of range. Well what will it do? (largest-smallest int, random...)
[color=blue]
>[color=green]
>>Are there caveeats/pits?
>>(Apart from the range and the precision of course)[/color]
>
>
> Again, that depends on what you expect.[/color]
That the conversion is done and that homogenously over the whole value
range of the int.
[color=blue][color=green]
>>What would be the best way to do it?[/color]
>
>
> If you want to remove the warning, do a cast:
>
> int main()
> {
> double d(3); int i;
>
> i = static_cast<int>(d);
> }[/color]
Depends on what it will do. Ie there isn't any caveeat.
marc | | | | re: assignmnet to int form float/double
Marc Schellens wrote:[color=blue]
> Rolf Magnus wrote:[color=green]
>> Marc Schellens wrote:
>>
>>[color=darkred]
>>> My compiler warns about assignment to int form
>>> float/double.
>>> Will it nevertheless do what I expect?[/color]
>>
>>
>> Depends on what you expect.[/color]
>
> Come on, thats obvious:
> I want an integer value close to the float value (if its in the
> range).
> If its out of range. Well what will it do? (largest-smallest int,
> random...)[/color]
The trouble with "obvious" is what is obvious to you may not be obvious to
other people. People tend to have different ideas about what is obvious and
what is not.
To answer your question, the conversion truncates; the fractional part is
discarded. If the truncated value cannot be represented in the integer type
("out of range"), the result is undefined. I.e. you will have to do the
range checking yourself.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl | | | | re: assignmnet to int form float/double
Marc Schellens wrote:
[color=blue]
> Rolf Magnus wrote:[color=green]
>> Marc Schellens wrote:
>>
>>[color=darkred]
>>>My compiler warns about assignment to int form
>>>float/double.
>>>Will it nevertheless do what I expect?[/color]
>>
>>
>> Depends on what you expect.[/color]
>
> Come on, thats obvious:
> I want an integer value close to the float value (if its in the
> range).[/color]
It's not so obvious what "close to" means exactly. Do you expect the
fractional part to be discarded, sometimes called
"round-towards-zero" (which is what actually happens), or round-down,
round-up, round-to-nearest?
[color=blue]
> If its out of range. Well what will it do? (largest-smallest
> int, random...)[/color]
Undefined in C++.
[color=blue][color=green][color=darkred]
>>>Are there caveeats/pits?
>>>(Apart from the range and the precision of course)[/color]
>>
>>
>> Again, that depends on what you expect.[/color]
>
> That the conversion is done and that homogenously over the whole value
> range of the int.[/color]
Well, that's not exactly what I meant. An example:
float f = 0.1f;
f *= 10.0f;
int i = static_cast<int>(f);
You might expect i to contain 1 now, but depending on the
implementation, it might instead contain 0. That's because 0.1f cannot
be represented exactly as a floating point value, so the variable will
contain a value slightly greater or smaller than 0.1. That means that
the result of the multiplication might e.g. be someting like 0.999998,
in which case discarding the frational part results in 0. If you expect
round-to-nearest, you'd expect a result of 1.
[color=blue][color=green][color=darkred]
>>>What would be the best way to do it?[/color]
>>
>>
>> If you want to remove the warning, do a cast:
>>
>> int main()
>> {
>> double d(3); int i;
>>
>> i = static_cast<int>(d);
>> }[/color]
>
> Depends on what it will do. Ie there isn't any caveeat.[/color]
Well, there is none, if you know what to expect ;-) | | | | re: assignmnet to int form float/double
Obvious or not, that was what I wanted to know.
Thanks,
marc | | | | re: assignmnet to int form float/double
Marc Schellens wrote:[color=blue]
> I want an integer value close to the float value (if its in the range).[/color]
Floating-point values are converted to integral values by truncating and
discarding fractional of the original value part. If the resultant value
does not fit into the destination type, the behavior is undefined.
[color=blue]
> If its out of range. Well what will it do? (largest-smallest int, random...)[/color]
Undefined behavior. On some platforms the program will crash.
--
Best regards,
Andrey Tarasevich | | | | re: assignmnet to int form float/double
Andrey Tarasevich wrote:[color=blue]
> Marc Schellens wrote:
>[color=green]
>>I want an integer value close to the float value (if its in the range).[/color]
>
>
> Floating-point values are converted to integral values by truncating and
> discarding fractional of the original value part. If the resultant value
> does not fit into the destination type, the behavior is undefined.
>
>[color=green]
>>If its out of range. Well what will it do? (largest-smallest int, random...)[/color]
>
>
> Undefined behavior. On some platforms the program will crash.
>[/color]
This would make of course a big impact.
Any common platform? Ie. workstation with unix, windows or mac OS?
Or did you mean 'might' crash (more theoretical)?
marc | | | | re: assignmnet to int form float/double
"Marc Schellens" <m_schellens@hotmail.com> wrote in message
news:3FD49099.5000004@hotmail.com...[color=blue]
> Andrey Tarasevich wrote:[color=green]
> > Marc Schellens wrote:
> >[color=darkred]
> >>I want an integer value close to the float value (if its in the range).[/color]
> >
> > Floating-point values are converted to integral values by truncating[/color][/color]
and[color=blue][color=green]
> > discarding fractional of the original value part. If the resultant[/color][/color]
value[color=blue][color=green]
> > does not fit into the destination type, the behavior is undefined.
> >[color=darkred]
> >>If its out of range. Well what will it do? (largest-smallest int,[/color][/color][/color]
random...)[color=blue][color=green]
> >
> > Undefined behavior. On some platforms the program will crash.[/color]
>
> This would make of course a big impact.
> Any common platform? Ie. workstation with unix, windows or mac OS?
> Or did you mean 'might' crash (more theoretical)?[/color]
If you do range checking you don't have to worry about what will happen
with out-of-range conversions. With range checking it is up to you how to
handle out-of-range situations; you could clip the value to the
maximum/minimum value that can be represented by an int, or you could throw
a std::range_error or std::out_of_range exception, or whatever other action
that is the most appropriate given your situation.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl | | | | re: assignmnet to int form float/double
Marc Schellens wrote:[color=blue][color=green]
>> ...[color=darkred]
>>>If its out of range. Well what will it do? (largest-smallest int, random...)[/color]
>>
>>
>> Undefined behavior. On some platforms the program will crash.
>>[/color]
>
> This would make of course a big impact.
> Any common platform? Ie. workstation with unix, windows or mac OS?
> Or did you mean 'might' crash (more theoretical)?
> ...[/color]
I remember running into this problem on Solaris 5.7 with SunWorkshop Pro
6.1 C compiler. What I don't remember is whether the conversion from
out-of-range 'double' to 'unsigned int' caused an immediate coredump or
the program crashed somewhere later.
--
Best regards,
Andrey Tarasevich |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|