1-line-puzzle: output 64-bit integer 
Author Message
 1-line-puzzle: output 64-bit integer

Is there any way to output a 64 bit int on a 32-bit machine as an
integer ?
( I suppose not, since I grepped for "long long" and "int64" in the awk
source directories.)

- This generates fine output:
/ > echo doit | awk "{string=\"1\"; printf(\"%032i\n\",string); }"
00000000000000000000000000000001

- While this does not do what we hoped for: ( 0xefffffffffffffff ==
17293822569102704639 )
/ > echo doit | awk "{string=\"17293822569102704639\";
printf(\"%032i\",string); }"
0000000000000000000001.72938e+19

By the way, what we would have liked to see is:
00000000000017293822569102704639

Thank you



Sun, 14 Dec 2003 23:42:28 GMT  
 1-line-puzzle: output 64-bit integer

Quote:
> ( I suppose not, since I grepped for "long long" and "int64" in the awk
> source directories.)

"All numbers are represented internally  and  all  computations are done
in floating point arithmetic."

Quote:
> - While this does not do what we hoped for: ( 0xefffffffffffffff ==
> 17293822569102704639 )
> / > echo doit | awk "{string=\"17293822569102704639\";
> printf(\"%032i\",string); }"
> 0000000000000000000001.72938e+19

> By the way, what we would have liked to see is:
> 00000000000017293822569102704639

I think you will need to use a trick, as there is no long long formater
in printf. This task will probably require a separate function.

--
BBP



Sun, 14 Dec 2003 23:54:08 GMT  
 1-line-puzzle: output 64-bit integer

Quote:

>> ( I suppose not, since I grepped for "long long" and "int64" in the awk
>> source directories.)

> "All numbers are represented internally  and  all  computations are done
> in floating point arithmetic."

I did not find this sentence in "Edition 3 of `GAWK: Effective AWK
Programming: A User's Guide for GNU Awk', for the 3.1.0 version of the
GNU implementation of AWK", but I found:

   Floating-point numbers represent what are called "real" numbers;
i.e., those that do have a fractional part, such as 3.1415927.  The
advantage to floating-point numbers is that they can represent a much
larger range of values.  The disadvantage is that there are numbers
that they cannot represent exactly.  `awk' uses "double-precision"
floating-point numbers, which can hold more digits than
"single-precision" floating-point numbers.  Floating-point issues are
discussed more fully in *Note Floating-Point Number Caveats: Floating
Point Issues.
<<<

Quote:
>> - While this does not do what we hoped for: ( 0xefffffffffffffff ==
>> 17293822569102704639 )
>> / > echo doit | awk "{string=\"17293822569102704639\";
>> printf(\"%032i\",string); }"
>> 0000000000000000000001.72938e+19

>> By the way, what we would have liked to see is:
>> 00000000000017293822569102704639

> I think you will need to use a trick, as there is no long long formater
> in printf. This task will probably require a separate function.

It's not only a formatting issue:

% awk "BEGIN{x=\"17293822569102704639\";y=\"17293822569102704638\";
  printf(\"%d\n\",x-y); }"
0

The strings are converted to double precision floating point numbers,
but this precision obviously is not high enough to hold all of the
digits. I guess the easiest way to do such calculations is using "bc":

% echo "17293822569102704639-17293822569102704638" | bc -l

Regards...
                Michael



Tue, 16 Dec 2003 15:44:32 GMT  
 1-line-puzzle: output 64-bit integer

Quote:

> It's not only a formatting issue:

> % awk "BEGIN{x=\"17293822569102704639\";y=\"17293822569102704638\";
>  printf(\"%d\n\",x-y); }"
> 0

Arnold Robbins asked me to forward a mail from him to comp.lang.awk,
because he can't post at the moment:

| The way to solve the O.P.'s problem is straightforward but a bit unusual:  
|
|         awk 'BEGIN { printf "%032.0f\n", 17293822569102704639 }'
|
| Using the %f format gets the full decimal value, with the precision
| of zero means no decimal point, and the '0' flags gives all the
| leading zeros.

I tried that with gawk 3.0.3 to 3.1.0, mawk and bwk's awk on Linux
(glibc-2.1.3) and a DJGPP-compiled gawk-3.1.0 on DOS, and I always get:

C:\>awk "BEGIN { printf \"%032.0f\n\", 17293822569102704639 }"
00000000000017293822569102704640

Even with some "similar" numbers, I get the same result:

%    gawk 'BEGIN { printf "%032.0f\n", 17293822569102704630 }'
00000000000017293822569102704640

%    gawk 'BEGIN { printf "%032.0f\n", 17293822569102704600 }'
00000000000017293822569102704640

%    gawk 'BEGIN { printf "%032.0f\n", 17293822569102704000 }'
00000000000017293822569102704640

Hmm, my machine seems to love that number. An unknown awk on HP-UX 11.0
prints:

$     awk 'BEGIN { printf "%032.0f\n", 17293822569102704639 }'
00000000000017293822569102704600

Regards...
                Michael



Tue, 16 Dec 2003 18:34:59 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. first bit set in a 64 bit integer...

2. REQUEST: 64-bit integer math on 32-bit processor

3. top 32 bits of 64-bit product of two 32-bit integers

4. 64-Bit Hexadecimal to 64-Bit Binary

5. x86-64 performance: 32 bit vs 64 bit

6. 64 bit integer math routines

7. 64-bit integer support in M2

8. Are 64 bit integers supported in PL1 ?

9. 64-bit integers in Ada

10. 64 bit integers

11. Fast unsigned 64-bit integer division?

12. Q: 64 bit integer math library?

 

 
Powered by phpBB® Forum Software