Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

best way to get the whole number of a double

68 views
Skip to first unread message

Lynn McGuire

unread,
Oct 27, 2022, 6:02:09 PM10/27/22
to
What is the best way to get the whole number of a double ?

For instance:

double x = 1.2;
double y = aint (x); // puts 1.0 in y

Thanks,
Lynn

Lynn McGuire

unread,
Oct 27, 2022, 6:16:33 PM10/27/22
to

Kaz Kylheku

unread,
Oct 27, 2022, 6:23:07 PM10/27/22
to
Do you want -1.2 to -2.0 even though -1.0 is closer?

C99 provides a function called round.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Andrey Tarasevich

unread,
Oct 27, 2022, 7:03:28 PM10/27/22
to
On 10/27/2022 3:01 PM, Lynn McGuire wrote:
> What is the best way to get the whole number of a double ?
>
> For instance:
>
>    double x = 1.2;
>    double y = aint (x);   // puts 1.0 in y
>

Get into what recipient? Another `double`? And integral variable?

--
Best regards,
Andrey



Andrey Tarasevich

unread,
Oct 27, 2022, 7:08:23 PM10/27/22
to
`floor`? `floor`???

`trunc`, obviously, not `floor`

--
Best regards,
Andrey




Keith Thompson

unread,
Oct 27, 2022, 7:22:28 PM10/27/22
to
Lynn McGuire <lynnmc...@gmail.com> writes:
> What is the best way to get the whole number of a double ?
>
> For instance:
>
> double x = 1.2;
> double y = aint (x); // puts 1.0 in y

Step 1: Define exactly what "the whole number of" means.

Your example stores the result in a double object. See section 7.12.9
of the C11 standard: ceil, floor, round, trunc, and a few others.

Just converting (perhaps implicitly) a double value to any integer type
truncates toward zero, but if the result is out of range the behavior is
undefined.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
[Watch this space]
void Void(void) { Void(); } /* The recursive call of the void */

Kenny McCormack

unread,
Oct 27, 2022, 8:22:55 PM10/27/22
to
In article <tjf30q$2s7v8$1...@dont-email.me>,
"trunc" sounds right, but the man page says it rounds. Which is not really
the same thing. You want a function such that f(-1.9) is -1.

My first reaction on seeing this thread was simply:

double y = (int) x;

but of course that has problems. You would want to use the largest integer
type your platform has, but even then, it probably won't be big enough for
all possible double values.

--
1/20/17: A great day for all those people who are sick of being told
they don't know how to spell "you're" (or "there").

Lynn McGuire

unread,
Oct 27, 2022, 8:46:26 PM10/27/22
to
On 10/27/2022 6:03 PM, Andrey Tarasevich wrote:
> On 10/27/2022 3:01 PM, Lynn McGuire wrote:
>> What is the best way to get the whole number of a double ?
>>
>> For instance:
>>
>>     double x = 1.2;
>>     double y = aint (x);   // puts 1.0 in y
>>
>
> Get into what recipient? Another `double`? And integral variable?

Yes, the recipient is a double.

Thanks,
Lynn

James Kuyper

unread,
Oct 28, 2022, 12:15:29 PM10/28/22
to
On 10/27/22 18:01, Lynn McGuire wrote:
> What is the best way to get the whole number of a double ?
>
> For instance:
>
> double x = 1.2;
> double y = aint (x); // puts 1.0 in y

C provides a variety of options, precisely because people have a wide
variety of different preferences for what "whole number of a double"
means. If you fill in the following chart, we can help you decide which
one you want:

Obey current rounding mode: yes/no
Raise "inexact" floating point exception if result != x: yes/no

x | Desired value of y
----+-------------------
-1.2|
1.2| 1.0
1.5|
1.7|
2.5|


Options include:
ceil(x): smallest integer greater than or equal to x
floor(x): greatest integer less than or equal to x
nearbyint(x): as specified by current rounding mode
rint(x): like nearbyint(), but "inexact" floating point exception is
raised if the result differs from x.
round(x): nearest integer to x, with halfway cases rounding toward 0.
roundeven(x): nearest integer to x, with halfway cases rounding to
nearest even number
trunc(x): round non-integer values to the bracketing integer closest to 0.

Fred J. Tydeman

unread,
Oct 29, 2022, 9:10:28 AM10/29/22
to
Besides calling one of the <math.h> functions,
one can use a "magic" value as below:

/*
* Convert double to integer (as a double)
*/

#include <float.h> /* DBL_EPSILON */
#include <stdio.h> /* printf() */

int main(void){
const double magic = 1.0 / DBL_EPSILON;
double test[] = { 0.4, 0.6, 1.4, 1.6, 2.4, 2.6, 3.4, 3.6, 127.4, 127.6 };
volatile double d;
int i,j;

for( j=0; j<(int)(sizeof(test)/sizeof(test[0])); j++ ){
for( i=0; i<2; i++ ){
d = test[j];

if( 0.0 <= d ){ /* round to integer using magic */
d += magic;
d -= magic;
}else{
d -= magic;
d += magic;
}
(void)printf("%g %g\n", test[j], d);

test[j] = -test[j];
}/* i */
}/* j */
return 0;
}

--
---
Fred J. Tydeman Tydeman Consulting
tyd...@tybor.com Testing, numerics, programming
+1 (702) 608-6093 Vice-chair of PL22.11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.

Chris M. Thomasson

unread,
Oct 29, 2022, 4:02:05 PM10/29/22
to
On 10/27/2022 3:01 PM, Lynn McGuire wrote:
> What is the best way to get the whole number of a double ?
>
> For instance:
>
>    double x = 1.2;
>    double y = aint (x);   // puts 1.0 in y

How horrible is this crap, just quickly typed it out:
____________________________
#include <iostream>
#include <cmath>


double foo(double x)
{
bool a = std::signbit(x);
double r = a ? -1 : 1;
return std::floor(std::abs(x)) * r;
}


void test(double a)
{
double b = foo(a);

std::cout << "a = " << a << "\n";
std::cout << "b = " << b << "\n\n";
}


int main()
{
test(-124.456);
test(124.456);
test(-456.768);

test(-0.);
test(0.);

return 0;
}
____________________________

Has to be full of interesting caveats! :^/

Sorry.

Chris M. Thomasson

unread,
Oct 29, 2022, 4:08:34 PM10/29/22
to
On 10/29/2022 1:01 PM, Chris M. Thomasson wrote:
> On 10/27/2022 3:01 PM, Lynn McGuire wrote:
>> What is the best way to get the whole number of a double ?
>>
>> For instance:
>>
>>     double x = 1.2;
>>     double y = aint (x);   // puts 1.0 in y
>
> How horrible is this crap, just quickly typed it out:
[...]

Ahh shit! I posted C++ to comp.lang.c! God damn it.

Manu Raju

unread,
Oct 30, 2022, 12:56:45 AM10/30/22
to
Why not use something simple like so:

std::cout << int(-124.456) << std::endl;
std::cout << int(124.456) << std::endl;
std::cout << int(-456.768) << std::endl;
std::cout << int(456.768) << std::endl;

The OP wanted to strip everything after the decimal point as far as I
understand from the question.



Bonita Montero

unread,
Oct 30, 2022, 6:47:53 AM10/30/22
to
trunc() is simpler.

Lynn McGuire

unread,
Oct 30, 2022, 11:32:23 PM10/30/22
to
On 10/27/2022 5:22 PM, Kaz Kylheku wrote:
> On 2022-10-27, Lynn McGuire <lynnmc...@gmail.com> wrote:
>> On 10/27/2022 5:01 PM, Lynn McGuire wrote:
>>> What is the best way to get the whole number of a double ?
>>>
>>> For instance:
>>>
>>>    double x = 1.2;
>>>    double y = aint (x);   // puts 1.0 in y
>>>
>>> Thanks,
>>> Lynn
>>
>> Ah, floor.
>> https://www.tutorialspoint.com/c_standard_library/c_function_floor.htm
>
> Do you want -1.2 to -2.0 even though -1.0 is closer?
>
> C99 provides a function called round.

I think that floor provides a very close, if not exact, replacement of
the Fortran aint function.
https://gcc.gnu.org/onlinedocs/gfortran/AINT.html

Thanks !

Lynn

Ben Bacarisse

unread,
Oct 31, 2022, 7:41:59 AM10/31/22
to
No, I'd say that trunc is the closest C replacement. In fact, if you
#include <tgmath.h> trunc even matches the result type from the argument
type as AINT does.

If you don't care about negative numbers, floor will do, but given that
there is an almost exact replacement, I can't see why you would choose
a function that was not quite the same!

--
Ben.

Tony Oliver

unread,
Oct 31, 2022, 9:13:37 AM10/31/22
to
After reading that definition of AINT() (and assuming that everywhere it
says X it actually means A), I devised this C equivalent function:

double aint( double x )
{
return copysign( floor( fabs( x ) ), x );
}

Then, comparing its results against those from all the candidate library
functions previously mentioned, I discovered that its behaviour is
*identical to that of trunc()*.

[In hindsight, I noticed the the definition of AINT does used the words
"truncate(s)", but without providing any contextual definition.]

Thus, I believe, the function you are looking for to replace AINT() is

trunc()

(or, in C, one of its relatives: truncf() or truncl(), if required,
since you posted to comp.lang.c).

Kenny McCormack

unread,
Oct 31, 2022, 9:43:53 AM10/31/22
to
In article <63e7e243-2a3e-4096...@googlegroups.com>,
Tony Oliver <guinne...@gmail.com> wrote:
...
>Thus, I believe, the function you are looking for to replace AINT() is
>
> trunc()
>
>(or, in C, one of its relatives: truncf() or truncl(), if required,
>since you posted to comp.lang.c).

I guess you are right. As I noted earlier, trunc() *should* be the right
answer, but the documentation (at least on the systems I've checked it on)
led me astray. However, experimentation indicates that it *does* work as
one would expect it to.

"man trunc" says:

These functions round x to the nearest integer not larger in absolute value.

Now, to me, the word "round" implies rounding (surprise! surprise!) and
rounding means that a value of, say, 1.9 (or -1.9) would round to 2 (or -2).

It would have been much clearer if the man page had simply said:

These functions return the integer nearest x not larger in absolute value
than x.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Seneca

Kaz Kylheku

unread,
Oct 31, 2022, 1:49:33 PM10/31/22
to
On 2022-10-31, Kenny McCormack <gaz...@shell.xmission.com> wrote:
> "man trunc" says:
>
> These functions round x to the nearest integer not larger in absolute value.

You're reading something called the "Linux Programmer's Manual", which
is second or third hand information. (Second if it refers to a standard,
third if it cribs from library docs/behavior.)

Glibc has its own documentation, which says it more clearly for regular
people, using the usual "toward zero":

The trunc functions round x towards zero to the nearest integer
(returned in floating-point format). Thus, trunc (1.5) is 1.0 and
trunc (-1.5) is -1.0.

You know from the name what it does, because truncation refers
to discarding the fractional part. The fractional part of a negative
proper fraction is itself negative: -1 1/2 means -1 + (-1/2).

The man page has really poor wording because although it is
mathematically correct, the "nearest" word is being used in a dumb way
in the context of rounding/truncation.

Yes, the integers -4, -3, -2, -1, 0, 1, 2, 3, 4 are all smaller in terms
of absolute value (magnitude) than -4.5, right? Oh, and of all those, -4
is the nearest. You know, in case you were stupidly wondering whether
-3 or -2, or even 2 could possibly be the result of trunc(-4.5)?

The psychology is bad; if your eyes see "nearest integer" in the
documentation of a truncating and rounding function of some kind, you're
automatically thinking about "the nearest integer in either direction",
like between 1 and 2, 1 is the nearer of the two to 1.2.

This is actually one of the better man pages.

If you're in the mood for amusing ranting, read "man 7 regex".

Scott Lurndal

unread,
Oct 31, 2022, 2:30:03 PM10/31/22
to
Kaz Kylheku <864-11...@kylheku.com> writes:
>On 2022-10-31, Kenny McCormack <gaz...@shell.xmission.com> wrote:
>> "man trunc" says:
>>
>> These functions round x to the nearest integer not larger in absolute value.
>
>You're reading something called the "Linux Programmer's Manual", which
>is second or third hand information. (Second if it refers to a standard,
>third if it cribs from library docs/behavior.)

The C standard, with respect to automatic conversions:

"When a finite value of real floating type is converted to an integer type other than _Bool,
the fractional part is discarded (i.e., the value is truncated toward zero)."

While with respect to the trunc() function:

"The trunc functions round their argument to the integer value,
in floating format, nearest to but no larger in magnitude than the argument."

Steven G. Kargl

unread,
Oct 31, 2022, 3:51:10 PM10/31/22
to
On Mon, 31 Oct 2022 06:13:28 -0700, Tony Oliver wrote:

> On Monday, 31 October 2022 at 03:32:23 UTC, Lynn McGuire wrote:
>> On 10/27/2022 5:22 PM, Kaz Kylheku wrote:
>> > On 2022-10-27, Lynn McGuire <lynnmc...@gmail.com> wrote:
>> >> On 10/27/2022 5:01 PM, Lynn McGuire wrote:
>> >>> What is the best way to get the whole number of a double ?
>> >>>
>> >>> For instance:
>> >>>
>> >>> double x = 1.2;
>> >>> double y = aint (x); // puts 1.0 in y
>> >>>
>> >>> Thanks,
>> >>> Lynn
>> >>
>> >> Ah, floor.
>> >> https://www.tutorialspoint.com/c_standard_library/c_function_floor.htm
>> >
>> > Do you want -1.2 to -2.0 even though -1.0 is closer?
>> >
>> > C99 provides a function called round.
>> I think that floor provides a very close, if not exact, replacement of
>> the Fortran aint function.
>> https://gcc.gnu.org/onlinedocs/gfortran/AINT.html
>

Gfortran's documentation is not authorative.

Try the Fortran 2018 standard, where one finds

16.9.9 AINT (A [, KIND])

1 Description. Truncation toward 0 to a whole number.

2 Class. Elemental function.

3 Arguments.
A shall be of type real.
KIND (optional) shall be a scalar integer constant expression.

4 Result Characteristics. The result is of type real. If KIND is
present, the kind type parameter is that specified by the value
of KIND; otherwise, the kind type parameter is that of A.

5 Result Value. If |A| < 1, AINT(A) has the value 0; if |A| >= 1,
AINT(A) has a value equal to the integer whose magnitude is the
largest integer that does not exceed the magnitude of A and whose
sign is the same as the sign of A.

--
steve

james...@alumni.caltech.edu

unread,
Nov 1, 2022, 12:47:17 AM11/1/22
to
On Monday, October 31, 2022 at 9:43:53 AM UTC-4, Kenny McCormack wrote:
...
> "man trunc" says:
>
> These functions round x to the nearest integer not larger in absolute value.
>
> Now, to me, the word "round" implies rounding (surprise! surprise!) and

Correct.

> rounding means that a value of, say, 1.9 (or -1.9) would round to 2 (or -2).

Incorrect. Rounding refers to any of several different ways of taking a number and mapping it to one of the two bracketing numbers from a discrete set (in this case, the integers). See the Wikipedia article on Rounding for a fuller explanation of the term. trunc() implements one of those mappings. The result you give is consistent with several of the other mappings.
0 new messages