0

I am hesitant to post my code because this is for a school assignment, but basically I need to iterate through a uint64_t that can be very large.

So, I have a value that is basically going to be maybe 10 characters long. It originally was an int, and the code partially works (but doesn't finish creating the output it's supposed to) but then I'd get the error "warning: integer constant is so large that it is unsigned".

I'd then change that int to uint64_t (it's being multiplied by 10), and it would give me a floating point exception.

Any insight without the code?

EDIT: Adding little bits of code...

 uint64_t number = 1U;

 ...

 for (int x = 0; x < y; x++) {
     number *= 10;
 }

^ this gives me the floating point exception.

Not sure if this is enough to be helpful.

Laurence
  • 157
  • 1
  • 2
  • 10
  • show us some code to help us locate the true problem.:-) – MYMNeo Sep 27 '12 at 02:11
  • Are you seeing a compile time error (which is what it sounds like) or a runtime error? As a compile time error, there's no way you can get a 'number too big' error from the `1U`, `0` or `10`. So, you must have misidentified the line, or misquoted the line. As a runtime error, what you're seeing is most unlikely — I probably shouldn't even bring up the possibility. So, you need to look at other lines of code than the ones you've shown so far. On a 32-bit system, your constant would have to be over 2147483647 to get the 'so large it is unsigned' (over 32767 on a system with 16-bit `int`). – Jonathan Leffler Sep 27 '12 at 02:32

2 Answers2

0

By default, integer literals are signed. If you use the U suffix on the literal, that tells the compiler to make it unsigned, e.g. 123456789U. That should clear up the warning about the constant being too large. Alternatively, you could use the UINT64_C() macro from <stdint.h>.

It's hard to know what's causing your FPE without seeing some code.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you. I just tried that and it isn't working (still have the warning and the exception). I have `number *= 10;` with number being the uint64_t that I'm trying to work with. Could that be what is causing the error? – Laurence Sep 27 '12 at 02:06
0

Have you checked how many times of the for loop executed? In your for loop, the number increased with a exponential ratio.I wrote a small program to show the situation.

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdint.h>
  4 
  5 #define MAXLOOP 100
  6 
  7 int
  8 main(void)
  9 {
 10     uint64_t number = 1U;
 11     int i;
 12 
 13     for(i = 0; i < MAXLOOP; ++i){
 14         number *= 10;
 15         printf("%d\t%lu\n", i, number);
 16     }
 17 
 18     return 0;
 19 }
 20 

The output is:

0       10
1       100
2       1000
3       10000
4       100000
5       1000000
6       10000000
7       100000000
8       1000000000
9       10000000000
10      100000000000
11      1000000000000
12      10000000000000
13      100000000000000
14      1000000000000000
15      10000000000000000
16      100000000000000000
17      1000000000000000000
18      10000000000000000000
19      7766279631452241920 <--- overflow, uint64_t can store 18,446,744,073,709,551,616 as its max value
20      3875820019684212736
21      1864712049423024128
22      200376420520689664
23      2003764205206896640
24      1590897978359414784
25      15908979783594147840
26      11515845246265065472
27      4477988020393345024
28      7886392056514347008
29      5076944270305263616
30      13875954555633532928
31      9632337040368467968
32      4089650035136921600
33      4003012203950112768
34      3136633892082024448
35      12919594847110692864
36      68739955140067328
37      687399551400673280
38      6873995514006732800
39      13399722918938673152
40      4870020673419870208
41      11806718586779598848
42      7386721425538678784
43      80237960548581376
44      802379605485813760
45      8023796054858137600
46      6450984253743169536
47      9169610316303040512
48      17909126868192198656
49      13070572018536022016
50      1578511669393358848
51      15785116693933588480
52      10277214349659471872
53      10538423128046960640
54      13150510911921848320
55      2377900603251621888
56      5332261958806667264
57      16429131440647569408
58      16717361816799281152
59      1152921504606846976
60      11529215046068469760
61      4611686018427387904
62      9223372036854775808
63      0
64      0
65      0
66      0
67      0
68      0
69      0
70      0
71      0
72      0
73      0
74      0
75      0
76      0
77      0
78      0
79      0
80      0
81      0
82      0
83      0
84      0
85      0
86      0
87      0
88      0
89      0
90      0
91      0
92      0
93      0
94      0
95      0
96      0
97      0
98      0
99      0

I think it is easy for you to understand.:->

MYMNeo
  • 818
  • 5
  • 9