How is the precision loss from integer to float defined in C++?what happens at background when convert int to floatCan I call a constructor from another constructor (do constructor chaining) in C++?How do you declare an interface in C++?How can I profile C++ code running on Linux?round() for float in C++Precise floating-point<->string conversionNarrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?Is round-trip through floating point always defined behavior if floating point range is bigger?Convert large int to float without rounding c++
Can a new chain significantly improve the riding experience? If yes - what else can?
Honourable versus Right Honourable Members
Action queue manager to perform action in a FIFO fashion
Why do sellers care about down payments?
I asked for a graduate student position from a professor. He replied "welcome". What does that mean?
Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?
Insert str into larger str in the most pythonic way
Relevance of the Resurrection
Writing a love interest for my hero
Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?
How to find a missing person abroad
Dividing Divisive Divisors
Double it your way
Do Milankovitch Cycles fully explain climate change?
Kerning feedback on logo
Job offer without any details but asking me to withdraw other applications - is it normal?
Are CO₂ tire cartridges reusable for multiple tires?
Using the pipe operator ("|") when executing system commands
How to stabilise the bicycle seatpost and saddle when it is all the way up?
Why would "an mule" be used instead of "a mule"?
Writing a worded mathematical expression
Is the union of a chain of elementary embeddings elementary?
What is Japanese Language Stack Exchange called in Japanese?
Long list of Hit and Get from sudo apt update
How is the precision loss from integer to float defined in C++?
what happens at background when convert int to floatCan I call a constructor from another constructor (do constructor chaining) in C++?How do you declare an interface in C++?How can I profile C++ code running on Linux?round() for float in C++Precise floating-point<->string conversionNarrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?Is round-trip through floating point always defined behavior if floating point range is bigger?Convert large int to float without rounding c++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've a question to the code snippet below:
long l=9223372036854775807L;
float f=static_cast<float>(l);
The long value cannot be represanted exactly according to the IEEE754.
My Question is how is the lossy conversion handled:
- Is the nearest floating point representation taken?
- Is the next smaller/bigger representation taken?
- Or is an other approach is taken?
I'm aware of this question
what happens at background when convert int to float but this does not anwser my question.
c++ rounding
add a comment |
I've a question to the code snippet below:
long l=9223372036854775807L;
float f=static_cast<float>(l);
The long value cannot be represanted exactly according to the IEEE754.
My Question is how is the lossy conversion handled:
- Is the nearest floating point representation taken?
- Is the next smaller/bigger representation taken?
- Or is an other approach is taken?
I'm aware of this question
what happens at background when convert int to float but this does not anwser my question.
c++ rounding
2
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago
add a comment |
I've a question to the code snippet below:
long l=9223372036854775807L;
float f=static_cast<float>(l);
The long value cannot be represanted exactly according to the IEEE754.
My Question is how is the lossy conversion handled:
- Is the nearest floating point representation taken?
- Is the next smaller/bigger representation taken?
- Or is an other approach is taken?
I'm aware of this question
what happens at background when convert int to float but this does not anwser my question.
c++ rounding
I've a question to the code snippet below:
long l=9223372036854775807L;
float f=static_cast<float>(l);
The long value cannot be represanted exactly according to the IEEE754.
My Question is how is the lossy conversion handled:
- Is the nearest floating point representation taken?
- Is the next smaller/bigger representation taken?
- Or is an other approach is taken?
I'm aware of this question
what happens at background when convert int to float but this does not anwser my question.
c++ rounding
c++ rounding
edited 11 hours ago
user1235183
asked 12 hours ago
user1235183user1235183
1,28415 silver badges38 bronze badges
1,28415 silver badges38 bronze badges
2
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago
add a comment |
2
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago
2
2
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago
add a comment |
2 Answers
2
active
oldest
votes
C++ defines the conversion like this (quoting latest standard draft):
[conv.fpint]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.
The result is exact if possible.
If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.
[ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.
— end note
]
If the value being converted is outside the range of values that can be represented, the behavior is undefined.
If the source type is bool, the value false is converted to zero and the value true is converted to one.
The IEEE 754 standard defines conversion like this:
5.4.1 Arithmetic operations
It shall be possible to convert from all supported signed and unsigned integer formats to all supported arithmetic formats. Integral values are converted exactly from integer formats to floating-point formats whenever the value is representable in both formats. If the converted value is not exactly representable in the destination format, the result is determined according to the applicable rounding-direction attribute, and an inexact or floating-point overflow exception arises as specified in Clause 7, just as with arithmetic operations. The signs of integer zeros are preserved. Integer zeros without signs are converted to +0. The preferred exponent is 0.
Rounding modes are specified as:
4.3.1 Rounding-direction attributes to nearest
roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered.
roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.
4.3.2 Directed rounding attributes
roundTowardPositive, the result shall be the format’s floating-point number (possibly +∞) closest to and no less than the infinitely precise result
roundTowardNegative, the result shall be the format’s floating-point number (possibly −∞) closest to and no greater than the infinitely precise result
roundTowardZero, the result shall be the format’s floating-point number closest to and no greater in magnitude than the infinitely precise result.
4.3.3 Rounding attribute requirements
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results in binary formats.
So by default, your suggestion 1 would apply, but only if another mode hasn't been selected.
The C++ standard library inherits <cfenv> from the C standard. This header offers macros, functions and types for interacting with the floating point environment, including the rounding modes.
add a comment |
See here:
A prvalue of integer or unscoped enumeration type can be converted to
a prvalue of any floating-point type. If the value cannot be
represented correctly, it is implementation defined whether the
closest higher or the closest lower representable value will be
selected, although if IEEE arithmetic is supported, rounding defaults
to nearest. If the value cannot fit into the destination type, the
behavior is undefined. If the source type is bool, the value false is
converted to zero, and the value true is converted to one.
As for the rounding rules of IEEE 754, there seem to be five of them. I couldn't find any information on which ones are used in which situation, though. It looks like it's up to the implementation however, you can set the rounding mode in a C++ program as described here.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57870372%2fhow-is-the-precision-loss-from-integer-to-float-defined-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
C++ defines the conversion like this (quoting latest standard draft):
[conv.fpint]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.
The result is exact if possible.
If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.
[ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.
— end note
]
If the value being converted is outside the range of values that can be represented, the behavior is undefined.
If the source type is bool, the value false is converted to zero and the value true is converted to one.
The IEEE 754 standard defines conversion like this:
5.4.1 Arithmetic operations
It shall be possible to convert from all supported signed and unsigned integer formats to all supported arithmetic formats. Integral values are converted exactly from integer formats to floating-point formats whenever the value is representable in both formats. If the converted value is not exactly representable in the destination format, the result is determined according to the applicable rounding-direction attribute, and an inexact or floating-point overflow exception arises as specified in Clause 7, just as with arithmetic operations. The signs of integer zeros are preserved. Integer zeros without signs are converted to +0. The preferred exponent is 0.
Rounding modes are specified as:
4.3.1 Rounding-direction attributes to nearest
roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered.
roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.
4.3.2 Directed rounding attributes
roundTowardPositive, the result shall be the format’s floating-point number (possibly +∞) closest to and no less than the infinitely precise result
roundTowardNegative, the result shall be the format’s floating-point number (possibly −∞) closest to and no greater than the infinitely precise result
roundTowardZero, the result shall be the format’s floating-point number closest to and no greater in magnitude than the infinitely precise result.
4.3.3 Rounding attribute requirements
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results in binary formats.
So by default, your suggestion 1 would apply, but only if another mode hasn't been selected.
The C++ standard library inherits <cfenv> from the C standard. This header offers macros, functions and types for interacting with the floating point environment, including the rounding modes.
add a comment |
C++ defines the conversion like this (quoting latest standard draft):
[conv.fpint]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.
The result is exact if possible.
If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.
[ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.
— end note
]
If the value being converted is outside the range of values that can be represented, the behavior is undefined.
If the source type is bool, the value false is converted to zero and the value true is converted to one.
The IEEE 754 standard defines conversion like this:
5.4.1 Arithmetic operations
It shall be possible to convert from all supported signed and unsigned integer formats to all supported arithmetic formats. Integral values are converted exactly from integer formats to floating-point formats whenever the value is representable in both formats. If the converted value is not exactly representable in the destination format, the result is determined according to the applicable rounding-direction attribute, and an inexact or floating-point overflow exception arises as specified in Clause 7, just as with arithmetic operations. The signs of integer zeros are preserved. Integer zeros without signs are converted to +0. The preferred exponent is 0.
Rounding modes are specified as:
4.3.1 Rounding-direction attributes to nearest
roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered.
roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.
4.3.2 Directed rounding attributes
roundTowardPositive, the result shall be the format’s floating-point number (possibly +∞) closest to and no less than the infinitely precise result
roundTowardNegative, the result shall be the format’s floating-point number (possibly −∞) closest to and no greater than the infinitely precise result
roundTowardZero, the result shall be the format’s floating-point number closest to and no greater in magnitude than the infinitely precise result.
4.3.3 Rounding attribute requirements
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results in binary formats.
So by default, your suggestion 1 would apply, but only if another mode hasn't been selected.
The C++ standard library inherits <cfenv> from the C standard. This header offers macros, functions and types for interacting with the floating point environment, including the rounding modes.
add a comment |
C++ defines the conversion like this (quoting latest standard draft):
[conv.fpint]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.
The result is exact if possible.
If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.
[ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.
— end note
]
If the value being converted is outside the range of values that can be represented, the behavior is undefined.
If the source type is bool, the value false is converted to zero and the value true is converted to one.
The IEEE 754 standard defines conversion like this:
5.4.1 Arithmetic operations
It shall be possible to convert from all supported signed and unsigned integer formats to all supported arithmetic formats. Integral values are converted exactly from integer formats to floating-point formats whenever the value is representable in both formats. If the converted value is not exactly representable in the destination format, the result is determined according to the applicable rounding-direction attribute, and an inexact or floating-point overflow exception arises as specified in Clause 7, just as with arithmetic operations. The signs of integer zeros are preserved. Integer zeros without signs are converted to +0. The preferred exponent is 0.
Rounding modes are specified as:
4.3.1 Rounding-direction attributes to nearest
roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered.
roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.
4.3.2 Directed rounding attributes
roundTowardPositive, the result shall be the format’s floating-point number (possibly +∞) closest to and no less than the infinitely precise result
roundTowardNegative, the result shall be the format’s floating-point number (possibly −∞) closest to and no greater than the infinitely precise result
roundTowardZero, the result shall be the format’s floating-point number closest to and no greater in magnitude than the infinitely precise result.
4.3.3 Rounding attribute requirements
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results in binary formats.
So by default, your suggestion 1 would apply, but only if another mode hasn't been selected.
The C++ standard library inherits <cfenv> from the C standard. This header offers macros, functions and types for interacting with the floating point environment, including the rounding modes.
C++ defines the conversion like this (quoting latest standard draft):
[conv.fpint]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.
The result is exact if possible.
If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.
[ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.
— end note
]
If the value being converted is outside the range of values that can be represented, the behavior is undefined.
If the source type is bool, the value false is converted to zero and the value true is converted to one.
The IEEE 754 standard defines conversion like this:
5.4.1 Arithmetic operations
It shall be possible to convert from all supported signed and unsigned integer formats to all supported arithmetic formats. Integral values are converted exactly from integer formats to floating-point formats whenever the value is representable in both formats. If the converted value is not exactly representable in the destination format, the result is determined according to the applicable rounding-direction attribute, and an inexact or floating-point overflow exception arises as specified in Clause 7, just as with arithmetic operations. The signs of integer zeros are preserved. Integer zeros without signs are converted to +0. The preferred exponent is 0.
Rounding modes are specified as:
4.3.1 Rounding-direction attributes to nearest
roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered.
roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.
4.3.2 Directed rounding attributes
roundTowardPositive, the result shall be the format’s floating-point number (possibly +∞) closest to and no less than the infinitely precise result
roundTowardNegative, the result shall be the format’s floating-point number (possibly −∞) closest to and no greater than the infinitely precise result
roundTowardZero, the result shall be the format’s floating-point number closest to and no greater in magnitude than the infinitely precise result.
4.3.3 Rounding attribute requirements
The roundTiesToEven rounding-direction attribute shall be the default rounding-direction attribute for results in binary formats.
So by default, your suggestion 1 would apply, but only if another mode hasn't been selected.
The C++ standard library inherits <cfenv> from the C standard. This header offers macros, functions and types for interacting with the floating point environment, including the rounding modes.
edited 12 hours ago
answered 12 hours ago
eerorikaeerorika
106k6 gold badges87 silver badges163 bronze badges
106k6 gold badges87 silver badges163 bronze badges
add a comment |
add a comment |
See here:
A prvalue of integer or unscoped enumeration type can be converted to
a prvalue of any floating-point type. If the value cannot be
represented correctly, it is implementation defined whether the
closest higher or the closest lower representable value will be
selected, although if IEEE arithmetic is supported, rounding defaults
to nearest. If the value cannot fit into the destination type, the
behavior is undefined. If the source type is bool, the value false is
converted to zero, and the value true is converted to one.
As for the rounding rules of IEEE 754, there seem to be five of them. I couldn't find any information on which ones are used in which situation, though. It looks like it's up to the implementation however, you can set the rounding mode in a C++ program as described here.
add a comment |
See here:
A prvalue of integer or unscoped enumeration type can be converted to
a prvalue of any floating-point type. If the value cannot be
represented correctly, it is implementation defined whether the
closest higher or the closest lower representable value will be
selected, although if IEEE arithmetic is supported, rounding defaults
to nearest. If the value cannot fit into the destination type, the
behavior is undefined. If the source type is bool, the value false is
converted to zero, and the value true is converted to one.
As for the rounding rules of IEEE 754, there seem to be five of them. I couldn't find any information on which ones are used in which situation, though. It looks like it's up to the implementation however, you can set the rounding mode in a C++ program as described here.
add a comment |
See here:
A prvalue of integer or unscoped enumeration type can be converted to
a prvalue of any floating-point type. If the value cannot be
represented correctly, it is implementation defined whether the
closest higher or the closest lower representable value will be
selected, although if IEEE arithmetic is supported, rounding defaults
to nearest. If the value cannot fit into the destination type, the
behavior is undefined. If the source type is bool, the value false is
converted to zero, and the value true is converted to one.
As for the rounding rules of IEEE 754, there seem to be five of them. I couldn't find any information on which ones are used in which situation, though. It looks like it's up to the implementation however, you can set the rounding mode in a C++ program as described here.
See here:
A prvalue of integer or unscoped enumeration type can be converted to
a prvalue of any floating-point type. If the value cannot be
represented correctly, it is implementation defined whether the
closest higher or the closest lower representable value will be
selected, although if IEEE arithmetic is supported, rounding defaults
to nearest. If the value cannot fit into the destination type, the
behavior is undefined. If the source type is bool, the value false is
converted to zero, and the value true is converted to one.
As for the rounding rules of IEEE 754, there seem to be five of them. I couldn't find any information on which ones are used in which situation, though. It looks like it's up to the implementation however, you can set the rounding mode in a C++ program as described here.
edited 12 hours ago
answered 12 hours ago
BlazeBlaze
11.5k1 gold badge16 silver badges36 bronze badges
11.5k1 gold badge16 silver badges36 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57870372%2fhow-is-the-precision-loss-from-integer-to-float-defined-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
C++ does not mandate IEEE754, FYI.
– L. F.
12 hours ago