tags:

views:

226

answers:

4

how do we print a number that's greater than 2^32-1 with int and float? (is it even possible?)

+6  A: 

How does your variable contain a number that is greater than 2^32 - 1? Short answer: It'll probably be a specific data-structure and assorted functions (oh, a class?) that deal with this.

Given this data structure, how do we print it? With BigInteger_Print(BigInteger*) of course :)

Really though, there is no correct answer to this, as printing a number larger than 2^32-1 depends entirely upon how you're storing that number.

Pod
+1  A: 

If you are talking about int64 types, you can try %I64u, %I64d, %I64x, %llu, %lld

JustJeff
-1, what about 200 bits?
hasen j
@hasen j: dude! -1 really? yeah what about 200 bits, how the heck is supplying information on 64 harmful or inaccurate?
JustJeff
+1  A: 

On common hardware, the largest float is (2^128 - 2^104), so if it's smaller than that, you just use %f (or %g or %a) with printf( ).

For int64 types, JustJeff's answer is spot on.

The range of double (%f) extends to nearly 2^1024, which is really quite huge; on Intel hardware, when the long double (%Lf) type corresponds to 80-bit float, the range of that type goes up to 2^16384.

If you need larger numbers than that, you need to use a library (which will likely have its own print routines) or roll your own representation and provide your own printing support.

Stephen Canon
Let's not forget `long` (which is guaranteed to be at least 32 bytes, but _can_ be longer) and `long long` (which is guaranteed to be at least 64 bits).
Chris Lutz
Let's also not forget that float only has about 7 decimal digits precision, double about 15, and long double about 20 or so.
greyfade
-1. I think the question is not about 32 bits specifically; it's generally about large numbers that don't fit into the hardware type.
hasen j
I don't think it's clear what the question is about. I gave an answer for one interpretation. Not sure why that earns a -1. Shrug. If the questioner wants to clarify, [s]he'll get a better answer.
Stephen Canon
+1, because a partial answer that is correct as far as it goes is better than no answer at all, if you know what i mean..
JustJeff
+2  A: 

More theoretically: suppose you have a very very very large number stored somewhere somehow; if so, I suppose that you are somehow able to do math on that number, otherwise it would be quite pointless storing it.

If you can do math on it, just divide the bignumber by ten (10); store the remainder somewhere. Repeat until the result is smaller than 10. When it's smaller than ten, print it, then print the remainders, from the last to the first. Finish.

You can speed up things by dividing for the largest power of 10 that you are able to print without effort (on 32 bit, 1'000'000'000).

Edit: pseudo code:

#include <stdio.h>
#include <math.h>
#include <math_with_very_very_big_num.h>

int main(int argc, char **argv) {
  very_very_big_num bignum = someveryverybignum;
  very_very_big_num quot;
  int size = (int) floor(vvbn_log10(bignum)) + 1;
  char *result = calloc(size, sizeof(char));

  int i = 0;
  do {
    quot = vvbn_divide(bignum, 10);
    result[i++] = (char) vvbn_remainder(bignum, 10) + '0';
    bignum = quot;
  } while (vvbn_greater(bignum,  9));
  result[i] = (char) vvbn_to_i(bignum) + '0';
  while(i >= 0)
    printf("%c", result[i--]);
  printf("\n");
}

(I wrote this using long, than translating it with veryverybignum stuff; it worked with long, unluckily I cannot try this version, so please forgive me if I made transation errors...)

giorgian
Some simple pseudocode would make this answer awesome, but +1 anyway.
Chris Lutz
@Chris: you're right; fixed.
giorgian