Topic: Defect Report: Floating-integral conversions (4.9/1) ambiguously defined
Author: kuyper@wizard.net
Date: Mon, 13 Mar 2006 11:35:55 CST Raw View
Seungbeom Kim wrote:
> Section 4.9 [conv.fpint], paragraph 1 states: "An rvalue of a floating
> point type can be converted to an rvalue of an integer type. The
> conversion truncates; that is, the fractional part is discarded."
>
> Here, the concepts of "truncation" and "fractional part" seem to
> be used without precise definitions. When -3.14 is converted into
> an integer, is the truncation toward zero or away from zero? Is
> the fractional part -0.14 or 0.86? The standard seem to give no
> clear answer to these.
>
> One could argue that these words have definite meanings established
> outside the C++ context, but when I look up Wikipedia for these terms,
> I find that truncation is defined as done using the floor function
> and the fractional part of x is defined as x-floor(x). Therefore,
> the meanings of "truncation" and "fractional part" as in the current
> C++ standard are not common sense and needs to be defined precisely.
> http://en.wikipedia.org/wiki/Truncation
> http://en.wikipedia.org/wiki/Fractional_part
My understanding is that "truncation" refers to truncating the series
which defines a number in terms of the values of it's digits by
discarding all of the digits after the decimal point, which is
equivalent to setting them to 0. This corresponds to "rounding toward
zero", not "rounding downward". The digits after the decimal point are
what is called the "fractional part", and "discarding the fractional
part" refers to the process of setting those digits to 0. I suspect
that the section you cite from the standard was written by someone
sharing the same (mis?)conception. At the very least, this indicates
that there's a difference of opinion on the matter, which argues in
favor of clarification.
> Proposed Resolution:
> - Replace "truncates" with "truncates toward zero".
I'd prefer "rounds toward zero"; with my understanding of the word
"truncate", "truncates toward zero" would be redundant.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]