لم تعُد "مجموعات Google" تتيح المشاركات أو الاشتراكات الجديدة من Usenet. وسيبقى بالإمكان عرض المحتوى السابق.

Rounding of doubles

61 مرّة مشاهدة
التخطي إلى أول رسالة غير مقروءة

Sian Mountbatten

غير مقروءة،
19‏/03‏/2012، 2:40:02 م19‏/3‏/2012
إلى
Has anybody used the functions such as fegetround and fesetround. What
do these functions return?

I'm trying to find a function which will round a double to
(1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0
(2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0

Any help would be appreciated
--
Dr Sian Mountbatten
Algol 68 specialist
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

James Kuyper

غير مقروءة،
19‏/03‏/2012، 6:56:10 م19‏/3‏/2012
إلى
On 03/19/2012 02:40 PM, Sian Mountbatten wrote:
> Has anybody used the functions such as fegetround and fesetround. What
> do these functions return?

7.6p8, describing <fenv.h> says:
"Each of the macros
FE_DOWNWARD
FE_TONEAREST
FE_TOWARDZERO
FE_UPWARD
is defined if and only if the implementation supports getting and
setting the represented rounding direction by means of the fegetround
and fesetround functions. Additional implementation-defined rounding
directions, with macro definitions beginning with FE_ and an uppercase
letter,210) may also be specified by the implementation. The defined
macros expand to integer constant expressions whose values are distinct
nonnegative values."

7.6.3.1p3: "Description
The fegetround function returns the value of the rounding direction
macro representing the current rounding direction or a negative value if
there is no such rounding direction macro or the current rounding
direction is not determinable."

7.6.3.2p2: "Description
The fesetround function establishes the rounding direction represented
by its argument round. If the argument is not equal to the value of a
rounding direction macro, the rounding direction is not changed.

Returns
The fesetround function returns zero if and only if the requested
rounding direction was established."

> I'm trying to find a function which will round a double to
> (1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0

That is a popular rounding mode for certain kinds of analysis, but not
one defined by the standard. Your implementation might support it: look
at <fenv.h> to find out.

> (2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0

For this kind of rounding, you don't need to worry about the floating
point environment, because that is precisely the defined behavior for
conversion of a floating point value to a signed integer type, which is
by far the simplest way to perform the conversion, if the value is small
enough to fit in some integer type.

If you're looking at floating point values too large to be represented
by any signed integer type, it's unlikely that the floating point value
is represented with sufficient precision to make rounding necessary.
However, in the unlikely case that it does have sufficient precision,
trunc(), or more likely truncl(), has the behavior you desire,
regardless of the current rounding mode.

Barry Schwarz

غير مقروءة،
19‏/03‏/2012، 6:56:39 م19‏/3‏/2012
إلى
On Mon, 19 Mar 2012 13:40:02 -0500 (CDT), Sian Mountbatten
<poen...@fastmail.co.uk> wrote:

>Has anybody used the functions such as fegetround and fesetround. What
>do these functions return?

Do you not have a reference?

fesetround returns 0 if it was successful establishing the
requested rounding direction, non-zero otherwise.

fegetround returns the current rounding direction.

>
>I'm trying to find a function which will round a double to
>(1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0
>(2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0

Only the documentation for your system can tell you what options
fsetround supports. If you are interested in portability, you could
write trivial functions to do the rounding for you.

--
Remove del for email

Tim Rentsch

غير مقروءة،
25‏/03‏/2012، 10:44:09 م25‏/3‏/2012
إلى
Sian Mountbatten <poen...@fastmail.co.uk> writes:

> Has anybody used the functions such as fegetround and fesetround. What
> do these functions return?
>
> I'm trying to find a function which will round a double to
> (1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0
> (2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0

Here is a link for a document describing standard C (not quite
current, but close enough for your purposes I believe):

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Look in section 7.12.9, "Nearest integer functions". Probably
one or more of these functions approximate what you want.

If you want to know about fegetround and fesetround, read
section 7.6 in its entirety. The fe{get,set}round functions are
described in 7.6.3, but read all of 7.6 if you want to use them.

Unfortunately, you may be on your own for the corner cases for
more esoteric rounding modes. And, if you want something that
is guaranteed to work portably, it's virtually certain that
you'll have to add some code to check the corner cases (where an
x.5 value is being rounded), since implementations are not
required to support all the different rounding modes.


> Any help would be appreciated

Hopefully this message did.

Tim Rentsch

غير مقروءة،
25‏/03‏/2012، 10:44:24 م25‏/3‏/2012
إلى
James Kuyper <james...@verizon.net> writes:

> On 03/19/2012 02:40 PM, Sian Mountbatten wrote:
[snip]
>> I'm trying to find a function which will round a double to
>> (1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0
>
> That is a popular rounding mode for certain kinds of analysis, but not
> one defined by the standard. Your implementation might support it: look
> at <fenv.h> to find out.
>
>> (2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0
>
> For this kind of rounding, you don't need to worry about the floating
> point environment, because that is precisely the defined behavior for
> conversion of a floating point value to a signed integer type, which is
> by far the simplest way to perform the conversion, if the value is small
> enough to fit in some integer type. [snip elaboration]

I think you are misunderstanding his request. Converting
to an integer type truncates, but what he's looking for
is rounding. He said what happens in two x.5 cases, but
any cases that aren't x.5 are, I believe, desired to round
in the normal way, ie, the (now unambiguous) nearest integer.
0 رسالة جديدة