unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#13031: large numbers
@ 2012-11-29  8:42 Jozef Chraplewski
  2012-11-29 19:08 ` Noah Lavine
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Jozef Chraplewski @ 2012-11-29  8:42 UTC (permalink / raw)
  To: 13031

Hi,

It looks that guile returns incorrect results when it works with really big numbers.

I've found it during resolving problem 48 from Project Euler:

http://projecteuler.net/problem=48

The solution is trivial:

(define (problem-48 limit)
 (define (F)
   (let loop ((n 1)
              (sum 0))
     (if (<= n limit)
         (loop (+ n 1) (+ sum (expt n n)))
         sum)))

 (let* ((str (number->string (F)))
          (len (string-length str)))
   (substring str (- len 10) len)))

(display (problem-48 1000))
(newline)


The proper answer is 9110846700 but guile returns 6457854188

I've tested solution under other scheme implementations (MIT scheme, petite and mzscheme) and it works as it should.

Does such a big numbers in guile require any special treatment or just they are not supported?

Best,
Jozef




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
@ 2012-11-29 19:08 ` Noah Lavine
  2012-11-29 19:15 ` Stefan Israelsson Tampe
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Noah Lavine @ 2012-11-29 19:08 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

[-- Attachment #1: Type: text/plain, Size: 1082 bytes --]

No, they are meant to be supported without any special treatment. I don't
know what is happening in your example.


On Thu, Nov 29, 2012 at 3:42 AM, Jozef Chraplewski <jozef@applicake.com>wrote:

> Hi,
>
> It looks that guile returns incorrect results when it works with really
> big numbers.
>
> I've found it during resolving problem 48 from Project Euler:
>
> http://projecteuler.net/problem=48
>
> The solution is trivial:
>
> (define (problem-48 limit)
>  (define (F)
>    (let loop ((n 1)
>               (sum 0))
>      (if (<= n limit)
>          (loop (+ n 1) (+ sum (expt n n)))
>          sum)))
>
>  (let* ((str (number->string (F)))
>           (len (string-length str)))
>    (substring str (- len 10) len)))
>
> (display (problem-48 1000))
> (newline)
>
>
> The proper answer is 9110846700 but guile returns 6457854188
>
> I've tested solution under other scheme implementations (MIT scheme,
> petite and mzscheme) and it works as it should.
>
> Does such a big numbers in guile require any special treatment or just
> they are not supported?
>
> Best,
> Jozef
>
>
>

[-- Attachment #2: Type: text/html, Size: 1558 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
  2012-11-29 19:08 ` Noah Lavine
@ 2012-11-29 19:15 ` Stefan Israelsson Tampe
  2012-11-29 22:11 ` Ludovic Courtès
  2012-12-01  5:22 ` Mark H Weaver
  3 siblings, 0 replies; 13+ messages in thread
From: Stefan Israelsson Tampe @ 2012-11-29 19:15 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

[-- Attachment #1: Type: text/plain, Size: 1066 bytes --]

Hi,

Which version of guile do you use, I get the correct value for guile
2.0.5.91-de6d8

/Stefan


On Thu, Nov 29, 2012 at 9:42 AM, Jozef Chraplewski <jozef@applicake.com>wrote:

> Hi,
>
> It looks that guile returns incorrect results when it works with really
> big numbers.
>
> I've found it during resolving problem 48 from Project Euler:
>
> http://projecteuler.net/problem=48
>
> The solution is trivial:
>
> (define (problem-48 limit)
>  (define (F)
>    (let loop ((n 1)
>               (sum 0))
>      (if (<= n limit)
>          (loop (+ n 1) (+ sum (expt n n)))
>          sum)))
>
>  (let* ((str (number->string (F)))
>           (len (string-length str)))
>    (substring str (- len 10) len)))
>
> (display (problem-48 1000))
> (newline)
>
>
> The proper answer is 9110846700 but guile returns 6457854188
>
> I've tested solution under other scheme implementations (MIT scheme,
> petite and mzscheme) and it works as it should.
>
> Does such a big numbers in guile require any special treatment or just
> they are not supported?
>
> Best,
> Jozef
>
>
>

[-- Attachment #2: Type: text/html, Size: 1554 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
  2012-11-29 19:08 ` Noah Lavine
  2012-11-29 19:15 ` Stefan Israelsson Tampe
@ 2012-11-29 22:11 ` Ludovic Courtès
  2012-11-30 11:54   ` Jozef Chraplewski
  2012-12-01  5:22 ` Mark H Weaver
  3 siblings, 1 reply; 13+ messages in thread
From: Ludovic Courtès @ 2012-11-29 22:11 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

Hi,

Jozef Chraplewski <jozef@applicake.com> skribis:

> The solution is trivial:
>
> (define (problem-48 limit)
>  (define (F)
>    (let loop ((n 1)
>               (sum 0))
>      (if (<= n limit)
>          (loop (+ n 1) (+ sum (expt n n)))
>          sum)))
>
>  (let* ((str (number->string (F)))
>           (len (string-length str)))
>    (substring str (- len 10) len)))
>
> (display (problem-48 1000))
> (newline)
>
>
> The proper answer is 9110846700 but guile returns 6457854188

I also get 9110846700 with 2.0.6+ and 1.8.8+.  What version do you use?

(Bigloo 3.2a returns 5842605292, though.)

Ludo’.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-29 22:11 ` Ludovic Courtès
@ 2012-11-30 11:54   ` Jozef Chraplewski
  2012-11-30 16:27     ` Ludovic Courtès
  2012-11-30 17:33     ` Mark H Weaver
  0 siblings, 2 replies; 13+ messages in thread
From: Jozef Chraplewski @ 2012-11-30 11:54 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: 13031

Hey guys,

I use guile 2.0.6, compiled on macbook pro 7.1 osx 10.8.2

I've compiled guild via home-brew package manager.

Here is the link to brew's formula with compilation options:
https://github.com/mxcl/homebrew/blob/master/Library/Formula/guile.rb

Best,
Jozef
On Nov 29, 2012, at 11:11 PM, Ludovic Courtès <ludo@gnu.org> wrote:

> Hi,
> 
> Jozef Chraplewski <jozef@applicake.com> skribis:
> 
>> The solution is trivial:
>> 
>> (define (problem-48 limit)
>> (define (F)
>>   (let loop ((n 1)
>>              (sum 0))
>>     (if (<= n limit)
>>         (loop (+ n 1) (+ sum (expt n n)))
>>         sum)))
>> 
>> (let* ((str (number->string (F)))
>>          (len (string-length str)))
>>   (substring str (- len 10) len)))
>> 
>> (display (problem-48 1000))
>> (newline)
>> 
>> 
>> The proper answer is 9110846700 but guile returns 6457854188
> 
> I also get 9110846700 with 2.0.6+ and 1.8.8+.  What version do you use?
> 
> (Bigloo 3.2a returns 5842605292, though.)
> 
> Ludo’.






^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-30 11:54   ` Jozef Chraplewski
@ 2012-11-30 16:27     ` Ludovic Courtès
  2012-11-30 17:33     ` Mark H Weaver
  1 sibling, 0 replies; 13+ messages in thread
From: Ludovic Courtès @ 2012-11-30 16:27 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

Jozef Chraplewski <jozef@applicake.com> skribis:

> I use guile 2.0.6, compiled on macbook pro 7.1 osx 10.8.2

What compiler and what version of GMP is it?

Thanks,
Ludo’.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-30 11:54   ` Jozef Chraplewski
  2012-11-30 16:27     ` Ludovic Courtès
@ 2012-11-30 17:33     ` Mark H Weaver
  1 sibling, 0 replies; 13+ messages in thread
From: Mark H Weaver @ 2012-11-30 17:33 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: Ludovic Courtès, 13031

Jozef Chraplewski <jozef@applicake.com> writes:
> I use guile 2.0.6, compiled on macbook pro 7.1 osx 10.8.2
>
> I've compiled guild via home-brew package manager.

Could you please run "make check" in the build directory and show us the
output if any errors are reported?

    Thanks!
      Mark





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
                   ` (2 preceding siblings ...)
  2012-11-29 22:11 ` Ludovic Courtès
@ 2012-12-01  5:22 ` Mark H Weaver
  2012-12-03 16:35   ` Jozef Chraplewski
  3 siblings, 1 reply; 13+ messages in thread
From: Mark H Weaver @ 2012-12-01  5:22 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

Hi Jozef,

Jozef Chraplewski <jozef@applicake.com> writes:
> It looks that guile returns incorrect results when it works with really big numbers.

Please disregard my earlier request.  Can you please run the following
code and send us the output?

  (let ((modulus (expt 10 10)))
    (define (last10 n) (modulo n modulus))
    (let loop ((n 1) (sum 0))
      (if (> n 1000)
          (last10 sum)
          (let* ((term (expt n n))
                 (sum (+ sum term)))
            (format #t "~4@a ~10@a ~10@a~%" n (last10 term) (last10 sum))
            (loop (+ n 1) sum)))))

    Thanks,
      Mark





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-12-01  5:22 ` Mark H Weaver
@ 2012-12-03 16:35   ` Jozef Chraplewski
  2012-12-04  0:13     ` Mark H Weaver
  0 siblings, 1 reply; 13+ messages in thread
From: Jozef Chraplewski @ 2012-12-03 16:35 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 13031

[-- Attachment #1: Type: text/plain, Size: 1127 bytes --]

Hey guys,

Sorry for the late response, I was offline during the weekend.

@Mark, you can find output in attachment.

gcc -v

Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~67/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

brew info gmp

gmp: stable 5.0.5
http://gmplib.org/
/usr/local/Cellar/gmp/5.0.5 (11 files, 2.3M) *
https://github.com/mxcl/homebrew/commits/master/Library/Formula/gmp.rb
==> Options
--32-bit
	Build 32-bit only


Best,
Jozef


[-- Attachment #2: output.txt --]
[-- Type: text/plain, Size: 27000 bytes --]

   1          1          1
   2          4          5
   3         27         32
   4        256        288
   5       3125       3413
   6      46656      50069
   7     823543     873612
   8   16777216   17650828
   9  387420489  405071317
  10          0  405071317
  11 5311670611 5716741928
  12 6100448256 1817190184
  13 5106592253 6923782437
  14 6825558016 3749340453
  15  380859375 4130199828
  16          0 4130199828
  17 6336764177  466964005
  18 6537575424 7004539429
  19 3589123979  593663408
  20          0  593663408
  21 1381124421 1974787829
  22 6646723584 8621511413
  23 5032910567 3654421980
  24 1472843776 5127265756
  25 3447265625 8574531381
  26 3704360960 2278892341
  27 9149892803 1428785144
  28 4605812736 6034597880
  29 7744489485 3779087365
  30          0 3779087365
  31 8687357919 2466445284
  32          0 2466445284
  33 7183380513 9649825797
  34 1909569536 1559395333
  35 6435546875 7994942208
  36          0 7994942208
  37  199442517 8194384725
  38 6064610304 4258995029
  39 7009951959 1268946988
  40          0 1268946988
  41 3640524649 4909471637
  42  863718400 5773190037
  43 7198995507 2972185544
  44 6189367296 9161552840
  45 8173828125 7335380965
  46 7330356224 4665737189
  47 6835295183 1501032372
  48          0 1501032372
  49 9325062449  826094821
  50          0  826094821
  51 6283233899 7109328720
  52 2907575296   16904016
  53 1256150373 1273054389
  54 2383789056 3656843445
  55 2405234375 6062077820
  56 2050323456 8112401276
  57 4855688057 2968089333
  58 3028449280 5996538613
  59 2714209187 8710747800
  60          0 8710747800
  61 6354670125 5065417925
  62 2685932544 7751350469
  63 4166235071 1917585540
  64          0 1917585540
  65 3837890625 5755476165
  66 9453244416 5208720581
  67   52277723 5260998304
  68 7093733376 2354731680
  69 2201741429 4556473109
  70          0 4556473109
  71 5129996471 9686469580
  72 2322313216 2008782796
  73   62013833 2070796629
  74 2019388416 4090185045
  75 3544921875 7635106920
  76 4505991168 2141098088
  77 2530996797 4672094885
  78 6843454464 1515549349
  79 1339775919 2855325268
  80          0 2855325268
  81 1371782481 4227107749
  82 2078388224 6305495973
  83 7212640587 3518136560
  84          0 3518136560
  85 4814453125 8332589685
  86 4176935936 2509525621
  87 1160202919 3669728540
  88          0 3669728540
  89 6592384409  262112949
  90          0  262112949
  91 7006182019 7268294968
  92  933364736 8201659704
  93 5156944973 3358604677
  94 7661956096 1020560773
  95 3349609375 4370170148
  96          0 4370170148
  97 7810992225 2181162373
  98 2091295744 4272458117
  99 9200499899 3472958016
 100          0 3472958016
 101 6938023061  410981077
 102  720251904 1131232981
 103 5273189527 6404422508
 104 7625977856 4030400364
 105 5478515625 9508915989
 106 6682249216 6191165205
 107 4678361587  869526792
 108          0  869526792
 109 4996852829 5866379621
 110          0 5866379621
 111   80868239 5947247860
 112          0 5947247860
 113 8592817521 4540065381
 114 3398255616 7938320997
 115 3154296875 1092617872
 116  500617216 1593235088
 117 8730655141  323890229
 118          0  323890229
 119 8085808775 8409699004
 120          0 8409699004
 121 3628398521 2038097525
 122 7163689984 9201787509
 123 6246065507 5447853016
 124 1175781376 6623634392
 125  205078125 6828712517
 126 2759781376 9588493893
 127 5612797823 5201291716
 128          0 5201291716
 129 7361418369 2562710085
 130          0 2562710085
 131 8150392731  713102816
 132 5654040576 6367143392
 133 4633285813 1000429205
 134 9150048256  150477461
 135 7333984375 7484461836
 136 9484660736 6969122572
 137 9078068681 6047191253
 138 7613896704 3661087957
 139 7806491859 1467579816
 140          0 1467579816
 141 3454511741 4922091557
 142 6002494464  924586021
 143 4550116207 5474702228
 144          0 5474702228
 145 3369140625 8843842853
 146 5482423296 4326266149
 147 1739665163 6065931312
 148          0 6065931312
 149 4857937349  923868661
 150 7289243648 8213112309
 151 5014210151 3227322460
 152          0 3227322460
 153 4826004953 8053327413
 154 4366410752 2419738165
 155  263671875 2683410040
 156 2042255360 4725665400
 157  201583757 4927249157
 158 6655888384 1583137541
 159 7569000287 9152137828
 160          0 9152137828
 161 2215193761 1367331589
 162 4034617344 5401948933
 163  317095547 5719044480
 164 5193404416  912448896
 165 9163303125   75752021
 166 4707670016 4783422037
 167 2179163223 6962585260
 168 4470910976 1433496236
 169 4960889833 6394386069
 170          0 6394386069
 171 8252955571 4647341640
 172          0 4647341640
 173 4862786717 9510128357
 174 6675250176 6185378533
 175 6318359375 2503737908
 176          0 2503737908
 177 3320230321 5823968229
 178 5844170752 1668138981
 179 6328035819 7996174800
 180          0 7996174800
 181 3400068581 1396243381
 182 8038170624 9434414005
 183 8811465287 8245879292
 184 6072615936 4318495228
 185 4509765625 8828260853
 186 7158465536 5986726389
 187  906491683 6893218072
 188 5310340096 2203558168
 189 9799837357 2003395525
 190          0 2003395525
 191 3595298623 5598694148
 192          0 5598694148
 193 7221253313 2819947461
 194 2027933696 4847881157
 195 9873046875 4720928032
 196 4824964096 9545892128
 197 2158203125 1704095253
 198 8883334144  587429397
 199 7203999799 7791429196
 200          0 7791429196
 201 7204040201 4995469397
 202 6659426304 1654895701
 203 7460513427 9115409128
 204 6001555456 5116964584
 205 7236328125 2353292709
 206 2391033856 4744326565
 207 6898532143 1642858708
 208 8059196416 9702055124
 209 3395150289 3097205413
 210          0 3097205413
 211 3029444811 6126650224
 212 1584147456 7710797680
 213 4515086853 2225884533
 214 6719753216 8945637749
 215 5302734375 4248372124
 216 8730402816 2978774940
 217 3890391577 6869166517
 218 1271220224 8140386741
 219 5343243779 3483630520
 220          0 3483630520
 221 1055292621 4538923141
 222 6892816384 1431739525
 223 4469127967 5900867492
 224 2318718976 8219586468
 225 7900390625 6119977093
 226 7866878976 3986856069
 227 2818489403 6805345472
 228 8417063936 5222409408
 229 8551253269 3773662677
 230          0 3773662677
 231 8799556631 2573219308
 232 6076978176 8650197484
 233 8201731113 6851928597
 234 2104564736 8956493333
 235 6982421875 5938915208
 236 7374350336 3313265544
 237  853661917 4166927461
 238 8691503104 2858430565
 239 1391231759 4249662324
 240          0 4249662324
 241  559289841 4808952165
 242 7232932864 2041885029
 243 1363796907 3405681936
 244 7849322496 1255004432
 245 8876953125  131957557
 246 4600784896 4732742453
 247 8754387463 3487129916
 248 9680326656 3167456572
 249 1570312249 4737768821
 250          0 4737768821
 251 9226625251 3964394072
 252          0 3964394072
 253 1455078125 5419472197
 254          0 5419472197
 255 5765515007 1184987204
 256          0 1184987204
 257  712890625 1897877829
 258          0 1897877829
 259 9248046875 1145924704
 260          0 1145924704
 261 7898775861 9044700565
 262 5178527744 4223228309
 263 5600608247 9823836556
 264 2521302016 2345138572
 265 4541015625 6886154197
 266 8024815616 4910969813
 267 7343370323 2254340136
 268  374168576 2628508712
 269 3163711229 5792219941
 270          0 5792219941
 271 7262034671 3054254612
 272 1168060416 4222315028
 273 3181436433 7403751461
 274 6890187776 4293939237
 275 6591796875  885736112
 276 9740747776  626483888
 277 8579980869 9206464757
 278 8192635904 7399100661
 279 3761314279 1160414940
 280 4966674432 6127089372
 281 4452820569  579909941
 282 6368513024 6948422965
 283 3797249987  745672952
 284 6165313536 6910986488
 285 9267578125 6178564613
 286 9108315136 5286879749
 287 3179701983 8466581732
 288 5836573696 4303155428
 289 3580114209 7883269637
 290          0 7883269637
 291 6914448891 4797718528
 292 7896487936 2694206464
 293 8150261077  844467541
 294 1992467456 2836934997
 295 8551484375 1388419372
 296 1545885696 2934305068
 297 5584300137 8518605205
 298 6374892544 4893497749
 299  213499699 5106997448
 300          0 5106997448
 301  213590301 5320587749
 302 6229960704 1550548453
 303 8549010127   99558580
 304          0   99558580
 305 2323718705 2423277285
 306 7173907456 9597184741
 307 2446766443 2043951184
 308 5834326016 7878277200
 309 7725465189 5603742389
 310          0 5603742389
 311 3089811911 8693554300
 312 1149357056 9842911356
 313 2766163065 2609074421
 314          0 2609074421
 315 3701171875 6310246296
 316 6846908416 3157154712
 317 7101375277  258529989
 318  336922624  595452613
 319 1352703679 1948156292
 320          0 1948156292
 321 6337890625 8286046917
 322 2618102784  904149701
 323 8188776667 9092926368
 324  373656576 9466582944
 325 3408203125 2874786069
 326 4134398976 7009185045
 327 1739979703 8749164748
 328 3491009536 2240174284
 329 6138988169 8379162453
 330          0 8379162453
 331 3927347731 2306510184
 332          0 2306510184
 333 3627716413 5934226597
 334  478862336 6413088933
 335 2255859375 8668948308
 336  136359936 8805308244
 337 4317896273 3123204517
 338 7649429504  772634021
 339 8287171659 9059805680
 340          0 9059805680
 341 2845065861 1904871541
 342 9528331264 1433202805
 343 9251037607  684240412
 344 3088900096 3773140508
 345 3517066905 7290207413
 346 1196266496 8486473909
 347 9792229763 8278703672
 348 6007008256 4285711928
 349 7708239181 1993951109
 350          0 1993951109
 351 7471560351 9465511460
 352 4603012096 4068523556
 353 1107743073 5176266629
 354 7852921856 3029188485
 355 3310546875 6339735360
 356          0 6339735360
 357 2503115157 8842850517
 358 3793629184 2636479701
 359 1923641239 4560120940
 360          0 4560120940
 361 4294177961 8854298901
 362          0 8854298901
 363 5799880947 4654179848
 364 1125999616 5780179464
 365 4222028125    2207589
 366 8252681216 8254888805
 367 1606996623 9861885428
 368 4851506176 4713391604
 369  861846129 5575237733
 370 3985382400 9560620133
 371 2920233771 2480853904
 372          0 2480853904
 373 9181357733 1662211637
 374 3953125376 5615337013
 375 1240234375 6855571388
 376 2209125376 9064696764
 377 1082513593  147210357
 378 3933937664 4081148021
 379 8093825123 2174973144
 380          0 2174973144
 381 9971437421 2146410565
 382 6413415424 8559825989
 383 5252759167 3812585156
 384 8469211136 2281796292
 385 1962890625 4244686917
 386 7034484736 1279171653
 387 4517232283 5796403936
 388 2306487296 8102891232
 389 3984029109 2086920341
 390 5216000000 7302920341
 391 1088431991 8391352332
 392 6204609536 4595961868
 393 3106350793 7702312661
 394 2635088896  337401557
 395  419921875  757323432
 396 7887927296 8645250728
 397 4008147837 2653398565
 398 2117970944 4771369509
 399 5231999599    3369108
 400          0    3369108
 401 5232160401 5235529509
 402          0 5235529509
 403 6157666827 1393196336
 404 9229510656  622706992
 405 2939453125 3562160117
 406 3554349056 7116509173
 407 9371920743 6488429916
 408 2442735616 8931165532
 409 5784080089 4715245621
 410          0 4715245621
 411 9128499011 3843744632
 412 1027572736 4871317368
 413 1197721997 6069039365
 414 9017148416 5086187781
 415 5224609375  310797156
 416 4098134016 4408931172
 417 4531138977 8940070149
 418 7804545024 6744615173
 419 2067763579 8812378752
 420          0 8812378752
 421 3699940821 2512319573
 422 8723549184 1235868757
 423 7790785367 9026654124
 424   12594176 9039248300
 425 3603515625 2642763925
 426 9235634176 1878398101
 427 9960990387 1839388488
 428          0 1839388488
 429 7367623069 9207011557
 430          0 9207011557
 431  946058831  153070388
 432 9251173376 9404243764
 433 4093999793 3498243557
 434 4760359936 8258603493
 435   29296875 8287900368
 436 5178689536 3466589904
 437 4558048997 8024638901
 438 4839675904 2864314805
 439 9817311559 2681626364
 440          0 2681626364
 441 4025506041 6707132405
 442 7253092352 3960224757
 443 5822838307 9783063064
 444 5550877696 5333940760
 445 8330078125 3664018885
 446 4756868096 8420886981
 447  416192063 8837079044
 448          0 8837079044
 449 5745562049 4582641093
 450          0 4582641093
 451 7152015451 1734656544
 452 2465437696 4200094240
 453 4812163573 9012257813
 454 3492499456 2504757269
 455 9208984375 1713741644
 456 9101817856  815559500
 457  392750857 1208310357
 458 2719826944 3928137301
 459 3956187539 7884324840
 460          0 7884324840
 461  884400061 8768724901
 462  603628544 9372353445
 463 7725913647 7098267092
 464 1359497216 8457764308
 465 1494140625 9951904933
 466 5039266816 4991171749
 467 2153342923 7144514672
 468 5950923776 3095438448
 469 1279380229 4374818677
 470 3024000000 7398818677
 471 6790552871 4189371548
 472 8461167616 2650539164
 473 2593419033 5243958197
 474 2136062976 7380021173
 475 7138671875 4518693048
 476 5745022976  263716024
 477 4702683597 4966399621
 478 9427672064 4394071685
 479 1673215519 6067287204
 480          0 6067287204
 481 1865446881 7932734085
 482 9415732224 7348466309
 483 3973699387 1322165696
 484 5176308736 6498474432
 485 4773103125 1271577557
 486 8744974336   16551893
 487 7762082583 7778634476
 488 8368080896 6146715372
 489 4358644009  505359381
 490          0  505359381
 491 7223487603 7728846984
 492          0 7728846984
 493 7697520093 5426367077
 494 9900266496 5326633573
 495 8193359375 3519992948
 496 3739088896 7259081844
 497 8864375537 6123457381
 498 2626281472 8749738853
 499   62499499 8812238352
 500          0 8812238352
 501   62750501 8874988853
 502 9321109504 8196098357
 503 1497483527 9693581884
 504 2945888256 2639470140
 505  634765625 3274235765
 506 2456944640 5731180405
 507 9156995043 4888175448
 508 5132425216   20600664
 509 7453994989 7474595653
 510 2288000000 9762595653
 511 2557089279 2319684932
 512          0 2319684932
 513 9479583233 1799268165
 514 5489975296 7289243461
 515 6748046875 4037290336
 516  532079616 4569369952
 517 8126682677 2696052629
 518          0 2696052629
 519 9451423479 2147476108
 520          0 2147476108
 521 2043694921 4191171029
 522 7193155584 1384326613
 523 5686154067 7070480680
 524 5107531776 2178012456
 525 5361328125 7539340581
 526 6953291776 4492632357
 527 4337784303 8830416660
 528 4215540736 3045957396
 529  280157969 3326115365
 530          0 3326115365
 531 7898689931 1224805296
 532 8114430976 9339236272
 533 6282322757 5621559029
 534 6741057536 2362616565
 535 7177734375 9540350940
 536  709339136  249690076
 537 3781341017 4031031093
 538 2614242304 6645273397
 539 8104651459 4749924856
 540 6110720000  860644856
 541 3832944141 4693588997
 542 4530008064 9223597061
 543 4690199007 3913796068
 544 4947255296 8861051364
 545 1727258145  588309509
 546 5570589696 6158899205
 547 9389274363 5548173568
 548          0 5548173568
 549 8814436949 4362610517
 550          0 4362610517
 551 6470990551  833601068
 552 6536503296 7370104364
 553 9797706089 7167810453
 554          0 7167810453
 555  192525875 7360336328
 556 1901746176 9262082504
 557 4730966557 3993049061
 558 7102249984 1095299045
 559  873416143 1968715188
 560          0 1968715188
 561 7952442161 9921157349
 562 1332818944 1253976293
 563 4189706347 5443682640
 564          0 5443682640
 565 1139553125 6583235765
 566          0 6583235765
 567 4427091143 1010326908
 568 9720421376  730748284
 569 8507415929 9238164213
 570 5677286400 4915450613
 571 1235991971 6151442584
 572 5065481216 1216923800
 573 6260866093 7477789893
 574 4911000576 2388790469
 575 1162109375 3550899844
 576 6876440576  427340420
 577 2825902657 3253243077
 578 3406526464 6659769541
 579 5725075419 2384844960
 580          0 2384844960
 581  957412981 3342257941
 582 7022900224  365158165
 583 9186364087 9551522252
 584 7678606336 7230128588
 585 2666015625 9896144213
 586 2847783936 2743928149
 587 8957252883 1701181032
 588 5669354496 7370535528
 589 7226958909 4597494437
 590 1564388352 6161882789
 591 8621558191 4783440980
 592 8953972736 3737413716
 593 1552356177 5289769893
 594 1155844096 6445613989
 595 3466796875 9912410864
 596 2600561664 2512972528
 597 3539983237 6052955765
 598 8566687744 4619643509
 599 3307999399 7927642908
 600          0 7927642908
 601 3308360601 1236003509
 602 3557470208 4793473717
 603 1779460227 6572933944
 604 2639865856 9212799800
 605 2861787725 2074587525
 606          0 2074587525
 607 7945719199   20306724
 608 9151394816 9171701540
 609 3418209889 2589911429
 610          0 2589911429
 611 3371508411 5961419840
 612 6850425856 2811845696
 613  720956053 3532801749
 614 6017488896 9550290645
 615 5146484375 4696775020
 616 6553480192 1250255212
 617 6635006377 7885261589
 618 1753549824 9638811413
 619 6266683379 5905494792
 620          0 5905494792
 621 7936699997 3842194789
 622 7610921984 1453116773
 623  987478415 2440595188
 624          0 2440595188
 625 5556640625 7997235813
 626 8330469376 6327705189
 627 9073922603 5401627792
 628 3562126336 8963754128
 629 3719592869 2683346997
 630          0 2683346997
 631 3628241031 6311588028
 632 9903128576 6214716604
 633 8440912313 4655628917
 634 3165259776 7820888693
 635  537643875 8358532568
 636 5736308736 4094841304
 637 3704260717 7799102021
 638 5325128704 3124230725
 639  107384191 3231614916
 640          0 3231614916
 641 8907136641 2138751557
 642 9844286464 1983038021
 643 5498046875 7481084896
 644  190032896 7671117792
 645 6176403125 3847520917
 646 4725431296 8572952213
 647 8674476663 7247428876
 648 6613933056 3861361932
 649 4274811849 8136173781
 650          0 8136173781
 651 4431485651 2567659432
 652 3952208896 6519868328
 653 6414210173 2934078501
 654 3488454656 6422533157
 655 4130859375  553392532
 656 8311085056 8864477588
 657 2113399697  977877285
 658 1546553344 2524430629
 659 4692867339 7217297968
 660          0 7217297968
 661 6581304261 3798602229
 662 4371769344 8170371573
 663 7009911911 5180283484
 664 4120892416 9301175900
 665 9697265625 8998441525
 666  880598016 9879039541
 667 8906195523 8785235064
 668  207998976 8993234040
 669 3980850829 2974084869
 670          0 2974084869
 671 9419551071 2393635940
 672 8329634816  723270756
 673 9009961633 9733232389
 674 4949938176 4683170565
 675  185546875 4868717440
 676 7127279616 1995997056
 677 8068406997   64404053
 678 1022500864 1086904917
 679 5650335319 6737240236
 680          0 6737240236
 681 7922799081 4660039317
 682 4275482624 8935521941
 683 8274378163 7209900104
 684 6568103936 3778004040
 685 7467028125 1245032165
 686 8750913536 9995945701
 687 7323306319 7319252020
 688 3858308096 1177560116
 689 5911973809 7089533925
 690          0 7089533925
 691 7826701291 4916235216
 692 3187214336 8103449552
 693 2715678693  819128245
 694 4913821696 5732949941
 695 5995234375 1728184316
 696 3634533376 5362717692
 697 8221970937 3584688629
 698 4376326144 7961014773
 699 4371499299 2332514072
 700          0 2332514072
 701  830078125 3162592197
 702 2105698304 5268290501
 703  214596927 5482887428
 704  743443456 6226330884
 705   87890625 6314221509
 706 3214196736 9528418245
 707 2056040283 1584458528
 708 7747644416 9332102944
 709 5159724789 4491827733
 710          0 4491827733
 711 2571327031 7063154764
 712 1672595456 8735750220
 713 9289273353 8025023573
 714  879241216 8904264789
 715 7294921875 6199186664
 716 5140130816 1339317480
 717 5603110077 6942427557
 718 4487349248 1429776805
 719 6076543279 7506320084
 720          0 7506320084
 721 6589063121 4095383205
 722 8463522816 2558906021
 723 6000971467 8559877488
 724 3153406976 1713284464
 725 1064453125 2777737589
 726 1095166976 3872904565
 727  796580903 4669485468
 728          0 4669485468
 729 7328927769 1998413237
 730          0 1998413237
 731 3040247811 5038661048
 732 3673266176 8711927224
 733 8687057613 7398984837
 734 6968052736 4367037573
 735 7099609375 1466646948
 736   67598336 1534245284
 737 5612360417 7146605701
 738 9324335104 6470940805
 739 3182123579 9653064384
 740          0 9653064384
 741 4211480341 3864544725
 742          0 3864544725
 743 4972306967 8836851692
 744 9391210496 8228062188
 745 1728515625 9956577813
 746 4109392896 4065970709
 747 1982421875 6048392584
 748 5518534656 1566927240
 749 1929686749 3496613989
 750          0 3496613989
 751  836500751 4333114740
 752          0 4333114740
 753 4807743473 9140858213
 754 8998011904 8138870117
 755 6904296875 5043166992
 756 9974598656 5017765648
 757 9421137957 4438903605
 758 7748002816 2186906421
 759  285407239 2472313660
 760          0 2472313660
 761 8653986361 1126300021
 762 6910048256 8036348277
 763 8153458403 6189806680
 764 3352790016 9542596696
 765 6455078125 5997674821
 766 3231343616 9229018437
 767 1091808511  320826948
 768          0  320826948
 769 8905385729 9226212677
 770 8648166400 7874379077
 771 5304230171 3178609248
 772 2269628416 5448237664
 773  692442933 6140680597
 774 4124875776  265556373
 775 1083984375 1349540748
 776          0 1349540748
 777 1573079113 2922619861
 778 9427595264 2350215125
 779 1491995219 3842210344
 780          0 3842210344
 781  526316797 4368527141
 782 6618625024  987152165
 783 2057999599 3045151764
 784 1636801536 4681953300
 785   45646865 4727600165
 786 9737952256 4465552421
 787 5370553483 9836105904
 788  582941696  419047600
 789 4536688709 4955736309
 790          0 4955736309
 791  246098919 5201835228
 792 3187495936 8389331164
 793 9420667993 7809999157
 794 8886199296 6696198453
 795 4013671875  709870328
 796 9227293696 9937164024
 797 9897338637 9834502661
 798 8645484544 8479987205
 799 3455999199 1935986404
 800          0 1935986404
 801 3456640801 5392627205
 802 4793032704  185659909
 803 6013893627 6199553536
 804 8888621056 5088174592
 805 5595703125  683877717
 806 4557075456 5240953173
 807 5603118551  844071724
 808 2169174016 3013245740
 809 4961539689 7974785429
 810          0 7974785429
 811 7354043699 5328829128
 812 4249005056 9577834184
 813 2542330653 2120164837
 814 7816372224 9936537061
 815   68359375    4896436
 816 7410236416 7415132852
 817 2431640625 9846773477
 818 5388099584 5234873061
 819 3244003179 8478876240
 820          0 8478876240
 821 6161699941 4640576181
 822  626934784 5267510965
 823 3242420167 8509931132
 824 9848344576 8358275708
 825 8759765625 7118041333
 826 3775384576  893425909
 827 8701171875 9594597784
 828 4463937536 4058535320
 829 1551162669 5609697989
 830          0 5609697989
 831 7973953727 3583651716
 832          0 3583651716
 833 9485742913 3069394629
 834 4798350336 7867744965
 835 3623046875 1490791840
 836 3008408576 4499200416
 837 9212640117 3711840533
 838 2963861504 6675702037
 839 5057295799 1732997836
 840          0 1732997836
 841 5166015625 6899013461
 842 9619723264 6518736725
 843 1175641107 7694377832
 844          0 7694377832
 845 3486328125 1180705957
 846 6410474496 7591180453
 847 2094934191 9686114644
 848          0 9686114644
 849 2382061649 2068176293
 850          0 2068176293
 851 6289035851 8357212144
 852 2749321216 1106533360
 853 5129012357 6235545717
 854 8312446976 4547992693
 855 9052734375 3600727068
 856 7768032256 1368759324
 857 5107093657 6475852981
 858 9295581184 5771434165
 859 6004747139 1776181304
 860          0 1776181304
 861 6853488461 8629669765
 862 8340902912 6970572677
 863 8917644447 5888217124
 864          0 5888217124
 865 4150390625   38607749
 866 8732809216 8771416965
 867 3225928123 1997345088
 868 7529394176 9526739264
 869 8044020629 7570759893
 870          0 7570759893
 871 3653029271 1223789164
 872 3301462016 4525251180
 873 7143064233 1668315413
 874 3507813376 5176128789
 875 2732421875 7908550664
 876 2027516928 9936067592
 877 4442050397 4378117989
 878 7773809664 2151927653
 879 9230649487 1382577140
 880          0 1382577140
 881 6445831281 7828408421
 882 1396327424 9224735845
 883 5654296875 4879032720
 884 1876699136 6755731856
 885 5126953125 1882684981
 886 7980507136 9863192117
 887 8674683783 8537875900
 888 1491255296   29131196
 889 8024103609 8053234805
 890          0 8053234805
 891 7729147491 5782382296
 892 5659531264 1441913560
 893 6441597293 7883510853
 894 9984976896 7868487749
 895 3037109375  905597124
 896 7994935296 8900532420
 897 2353086337 1253618757
 898   22744064 1276362821
 899 2998046875 4274409696
 900          0 4274409696
 901  410351541 4684761237
 902 8295727104 2980488341
 903 4388350327 7368838668
 904 7907398656 5276237324
 905  791015625 6067252949
 906 7703469056 3770722005
 907 8556492243 2327214248
 908 1663983616 3991197864
 909 5906654589 9897852453
 910          0 9897852453
 911 2901734511 2799586964
 912          0 2799586964
 913 5419127953 8218714917
 914 8504636416 6723351333
 915  341796875 7065148208
 916 7055062016 4120210224
 917 6222063813  342274037
 918 9491457024 9833731061
 919 3018621799 2852352860
 920          0 2852352860
 921 9364911321 2217264181
 922  393181184 2610445365
 923 8421228867 1031674232
 924 9487282176  518956408
 925 5517578125 6036534533
 926 5699122176 1735656709
 927  224609375 1960266084
 928          0 1960266084
 929 2113060769 4073326853
 930          0 4073326853
 931 1411574651 5484901504
 932 5381861376  866762880
 933  575968213 1442731093
 934 9495847936  938579029
 935 7021484375 7960063404
 936 9875137536 7835200940
 937 1012099817 8847300757
 938 6595707904 5443008661
 939 9286011059 4729019720
 940          0 4729019720
 941 3073788829 7802808549
 942 2288881664   91690213
 943  551942223  643632436
 944 8116765696 8760398132
 945 3681640625 2442038757
 946 5116676096 7558714853
 947 6032803563 3591518416
 948 5069177856 8660696272
 949 7034936549 5695632821
 950          0 5695632821
 951 6307313479 2002946300
 952 3627165696 5630111996
 953 9474830073 5104942069
 954 5754387456  859329525
 955 7451171875 8310501400
 956 1241473024 9551974424
 957 7909629357 7461603781
 958 6296131584 3757735365
 959 4623100991 8380836356
 960          0 8380836356
 961 4662810561 3043646917
 962 7587180544  630827461
 963 7361952347 7992779808
 964 3278985216 1271765024
 965 9658203125  929968149
 966 1032994816 1962962965
 967 5700084023 7663046988
 968          0 7663046988
 969 6959755529 4622802517
 970          0 4622802517
 971   28948371 4651750888
 972  241135616 4892886504
 973 7700825533 2593712037
 974 3370750976 5964463013
 975 6005859375 1970322388
 976 5917310976 7887633364
 977 9074342097 6961975461
 978 7213144064 4175119525
 979 5096515019 9271634544
 980          0 9271634544
 981  489477381 9761111925
 982 3552589824 3313701749
 983 5150698279 8464400028
 984 5479796736 3944196764
 985 7822265625 1766462389
 986 2020324352 3786786741
 987 6101134083 9887920824
 988  231248896  119169720
 989 2097218509 2216388229
 990 2592000000 4808388229
 991 9962794015 4771182244
 992 6473179136 1244361380
 993 1917466593 3161827973
 994 4536172544 7698000517
 995 7060546875 4758547392
 996 7711696896 2470244288
 997 6176214037 8646458325
 998 4770361344 3416819669
 999 3041034519 6457854188
1000          0 6457854188

[-- Attachment #3: Type: text/plain, Size: 408 bytes --]


On Dec 1, 2012, at 6:22 AM, Mark H Weaver <mhw@netris.org> wrote:

>  (let ((modulus (expt 10 10)))
>    (define (last10 n) (modulo n modulus))
>    (let loop ((n 1) (sum 0))
>      (if (> n 1000)
>          (last10 sum)
>          (let* ((term (expt n n))
>                 (sum (+ sum term)))
>            (format #t "~4@a ~10@a ~10@a~%" n (last10 term) (last10 sum))
>            (loop (+ n 1) sum)))))


^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-12-03 16:35   ` Jozef Chraplewski
@ 2012-12-04  0:13     ` Mark H Weaver
  2012-12-05 16:20       ` Mark H Weaver
  0 siblings, 1 reply; 13+ messages in thread
From: Mark H Weaver @ 2012-12-04  0:13 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

[-- Attachment #1: Type: text/plain, Size: 877 bytes --]

Hi Jozef,

I guess that on your system, (* 65536 65536) evaluates to 0.
Is that right?

If so, I believe the problem is caused by an aggressive optimization in
recent versions of Clang, which breaks Guile's logic for detecting
overflow when multiplying two fixnums.

Currently, Guile computes kk = xx * yy and checks for overflow by
verifying that kk / xx == yy.

I believe that Clang is optimizing out the check, because recent C
standards permit C implementations to assume that signed integer
arithmetic will never overflow.  For details, see:
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

One solution is to compile with the "-fwrapv" option, which should
disable the optimization.

Another solution is to apply the following patch.

Jozef, would you be willing to test this patch and tell me if it fixes
the problem?

   Many thanks,
      Mark



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Fix for fixnum product overflow bug --]
[-- Type: text/x-diff, Size: 835 bytes --]

diff --git a/libguile/numbers.c b/libguile/numbers.c
index 52e227f..66c95db 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -7640,10 +7640,16 @@ scm_product (SCM x, SCM y)
       if (SCM_LIKELY (SCM_I_INUMP (y)))
 	{
 	  scm_t_inum yy = SCM_I_INUM (y);
-	  scm_t_inum kk = xx * yy;
-	  SCM k = SCM_I_MAKINUM (kk);
-	  if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
-	    return k;
+#if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
+          scm_t_int64 kk = xx * (scm_t_int64) yy;
+          if (SCM_FIXABLE (kk))
+            return SCM_I_MAKINUM (kk);
+#else
+          scm_t_inum axx = (xx > 0) ? xx : -xx;
+          scm_t_inum ayy = (yy > 0) ? yy : -yy;
+          if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
+            return SCM_I_MAKINUM (xx * yy);
+#endif
 	  else
 	    {
 	      SCM result = scm_i_inum2big (xx);

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-12-04  0:13     ` Mark H Weaver
@ 2012-12-05 16:20       ` Mark H Weaver
  2012-12-07 10:40         ` Jozef Chraplewski
  0 siblings, 1 reply; 13+ messages in thread
From: Mark H Weaver @ 2012-12-05 16:20 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031

I wrote:
> I guess that on your system, (* 65536 65536) evaluates to 0.
> Is that right?

I should have mentioned that if you're on a 64-bit system, then it may
instead be the case that (* (expt 2 32) (expt 2 32)) evaluates to 0.
Same bug either way, and the rest of my previous email still applies.

    Thanks,
      Mark


> If so, I believe the problem is caused by an aggressive optimization in
> recent versions of Clang, which breaks Guile's logic for detecting
> overflow when multiplying two fixnums.
>
> Currently, Guile computes kk = xx * yy and checks for overflow by
> verifying that kk / xx == yy.
>
> I believe that Clang is optimizing out the check, because recent C
> standards permit C implementations to assume that signed integer
> arithmetic will never overflow.  For details, see:
> http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
>
> One solution is to compile with the "-fwrapv" option, which should
> disable the optimization.
>
> Another solution is to apply the following patch.
>
> Jozef, would you be willing to test this patch and tell me if it fixes
> the problem?
>
>    Many thanks,
>       Mark
>
>
>
> diff --git a/libguile/numbers.c b/libguile/numbers.c
> index 52e227f..66c95db 100644
> --- a/libguile/numbers.c
> +++ b/libguile/numbers.c
> @@ -7640,10 +7640,16 @@ scm_product (SCM x, SCM y)
>        if (SCM_LIKELY (SCM_I_INUMP (y)))
>  	{
>  	  scm_t_inum yy = SCM_I_INUM (y);
> -	  scm_t_inum kk = xx * yy;
> -	  SCM k = SCM_I_MAKINUM (kk);
> -	  if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
> -	    return k;
> +#if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
> +          scm_t_int64 kk = xx * (scm_t_int64) yy;
> +          if (SCM_FIXABLE (kk))
> +            return SCM_I_MAKINUM (kk);
> +#else
> +          scm_t_inum axx = (xx > 0) ? xx : -xx;
> +          scm_t_inum ayy = (yy > 0) ? yy : -yy;
> +          if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
> +            return SCM_I_MAKINUM (xx * yy);
> +#endif
>  	  else
>  	    {
>  	      SCM result = scm_i_inum2big (xx);





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-12-05 16:20       ` Mark H Weaver
@ 2012-12-07 10:40         ` Jozef Chraplewski
  2012-12-07 17:06           ` Mark H Weaver
  0 siblings, 1 reply; 13+ messages in thread
From: Jozef Chraplewski @ 2012-12-07 10:40 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 13031

Hey Mark,

Yes, I use 64-bit machine and (* (expt 2 32) (expt 2 32)) produces 0 (without the patch).

I've applied your fix and it works perfectly.
I suppose that you will add the patch to the next stable version (2.0.8 ??)

Thanks for help and great work!

Best,
Jozef

On Dec 5, 2012, at 5:20 PM, Mark H Weaver <mhw@netris.org> wrote:

> I wrote:
>> I guess that on your system, (* 65536 65536) evaluates to 0.
>> Is that right?
> 
> I should have mentioned that if you're on a 64-bit system, then it may
> instead be the case that (* (expt 2 32) (expt 2 32)) evaluates to 0.
> Same bug either way, and the rest of my previous email still applies.
> 
>    Thanks,
>      Mark
> 
> 
>> If so, I believe the problem is caused by an aggressive optimization in
>> recent versions of Clang, which breaks Guile's logic for detecting
>> overflow when multiplying two fixnums.
>> 
>> Currently, Guile computes kk = xx * yy and checks for overflow by
>> verifying that kk / xx == yy.
>> 
>> I believe that Clang is optimizing out the check, because recent C
>> standards permit C implementations to assume that signed integer
>> arithmetic will never overflow.  For details, see:
>> http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
>> 
>> One solution is to compile with the "-fwrapv" option, which should
>> disable the optimization.
>> 
>> Another solution is to apply the following patch.
>> 
>> Jozef, would you be willing to test this patch and tell me if it fixes
>> the problem?
>> 
>>   Many thanks,
>>      Mark
>> 
>> 
>> 
>> diff --git a/libguile/numbers.c b/libguile/numbers.c
>> index 52e227f..66c95db 100644
>> --- a/libguile/numbers.c
>> +++ b/libguile/numbers.c
>> @@ -7640,10 +7640,16 @@ scm_product (SCM x, SCM y)
>>       if (SCM_LIKELY (SCM_I_INUMP (y)))
>> 	{
>> 	  scm_t_inum yy = SCM_I_INUM (y);
>> -	  scm_t_inum kk = xx * yy;
>> -	  SCM k = SCM_I_MAKINUM (kk);
>> -	  if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
>> -	    return k;
>> +#if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
>> +          scm_t_int64 kk = xx * (scm_t_int64) yy;
>> +          if (SCM_FIXABLE (kk))
>> +            return SCM_I_MAKINUM (kk);
>> +#else
>> +          scm_t_inum axx = (xx > 0) ? xx : -xx;
>> +          scm_t_inum ayy = (yy > 0) ? yy : -yy;
>> +          if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
>> +            return SCM_I_MAKINUM (xx * yy);
>> +#endif
>> 	  else
>> 	    {
>> 	      SCM result = scm_i_inum2big (xx);






^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#13031: large numbers
  2012-12-07 10:40         ` Jozef Chraplewski
@ 2012-12-07 17:06           ` Mark H Weaver
  0 siblings, 0 replies; 13+ messages in thread
From: Mark H Weaver @ 2012-12-07 17:06 UTC (permalink / raw)
  To: Jozef Chraplewski; +Cc: 13031-done

Hi Jozef,

Jozef Chraplewski <jozef@applicake.com> writes:
> Yes, I use 64-bit machine and (* (expt 2 32) (expt 2 32)) produces 0 (without the patch).
>
> I've applied your fix and it works perfectly.

Excellent!  Thanks for bringing this problem to our attention,
and for helping us track it down :)

> I suppose that you will add the patch to the next stable version (2.0.8 ??)

Yes.

   Thanks again,
       Mark





^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2012-12-07 17:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
2012-11-29 19:08 ` Noah Lavine
2012-11-29 19:15 ` Stefan Israelsson Tampe
2012-11-29 22:11 ` Ludovic Courtès
2012-11-30 11:54   ` Jozef Chraplewski
2012-11-30 16:27     ` Ludovic Courtès
2012-11-30 17:33     ` Mark H Weaver
2012-12-01  5:22 ` Mark H Weaver
2012-12-03 16:35   ` Jozef Chraplewski
2012-12-04  0:13     ` Mark H Weaver
2012-12-05 16:20       ` Mark H Weaver
2012-12-07 10:40         ` Jozef Chraplewski
2012-12-07 17:06           ` Mark H Weaver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).