Floating-point to integer conversion
Fred Tydeman
uunet!ibmpa.awdpa.ibm.com!tydeman
Thu Jun 10 09:35:09 PDT 1993
Subject: Floating to integral conversion
My understanding of ANSI C 3.2.1.3, page 36, line 2:
"When a value of floating type is converted to integral type, the
fractional part is discarded."
means the same thing as truncate toward zero. So, for these four cases:
Integer Floating
result value to convert
3 3.9
3 3.1
3 3.0
-3 -3.0
-3 -3.1
-3 -3.9
the integer part is kept and the fractional part is ignored.
My understanding appears to be backed up by the Rationale in 3.2.1.3 on
page 35:
"There was strong agreement that floating values should truncate
toward zero when converted to an integral type, the specification
adopted in the Standard."
Yet, in reading the C++ draft proposal (X3J16/93-0010, Jan 28, 1993),
section 4.4 Floating and Integral, it says:
"Conversion of a floating value to an integral type truncates; that
is, the fractional part is discard. Such conversions are machine
dependent; for example, the direction of truncation of negative
numbers varies from machine to machine."
It appears that C++ allows truncate towards minus infinity, as in:
Integer Floating
result value to convert
3 3.9
3 3.1
3 3.0
-3 -3.0
-4 -3.1
-4 -3.9
Am I misunderstanding C, or is C++ changing the meaning from C?
These are not IBM's views, these are the personal ones of:
Fred Tydeman, IBM, Austin, Texas (512) 838-3322; fax (512) 838-3484
AIX S/6000 Math library architect & IBM's rep to NCEG (X3J11.1)
Internet: tydemanaibmpa.awdpa.ibm.com uucp: uunet!ibmsupt!tydeman
More information about the Numeric-interest
mailing list