assignmnet to int form float/double

M

Marc Schellens

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
 
M

Marc Schellens

Rolf said:
Marc Schellens wrote:




Depends on what you expect.

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...)
Again, that depends on what you expect.

That the conversion is done and that homogenously over the whole value
range of the int.
If you want to remove the warning, do a cast:

int main()
{
double d(3); int i;

i = static_cast<int>(d);
}

Depends on what it will do. Ie there isn't any caveeat.

marc
 
R

Rolf Magnus

Marc said:
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?

Depends on what you expect.
Are there caveeats/pits?
(Apart from the range and the precision of course)

Again, that depends on what you expect.
What would be the best way to do it?

If you want to remove the warning, do a cast:

int main()
{
double d(3); int i;

i = static_cast<int>(d);
}
 
P

Peter van Merkerk

Marc said:
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...)

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.
 
R

Rolf Magnus

Marc said:
Come on, thats obvious:
I want an integer value close to the float value (if its in the
range).

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?
If its out of range. Well what will it do? (largest-smallest
int, random...)

Undefined in C++.
That the conversion is done and that homogenously over the whole value
range of the int.

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.
Depends on what it will do. Ie there isn't any caveeat.

Well, there is none, if you know what to expect ;-)
 
M

Marc Schellens

Andrey said:
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.




Undefined behavior. On some platforms the program will crash.

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
 
A

Andrey Tarasevich

Marc said:
I want an integer value close to the float value (if its in the range).

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.
If its out of range. Well what will it do? (largest-smallest int, random...)

Undefined behavior. On some platforms the program will crash.
 
P

Peter van Merkerk

Marc Schellens said:
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)?

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::eek:ut_of_range exception, or whatever other action
that is the most appropriate given your situation.
 
A

Andrey Tarasevich

Marc said:
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)?
...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top