How to handle arbitrarily large integers

后端 未结 7 880
南旧
南旧 2020-12-01 11:34

I\'m working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the

7条回答
  •  自闭症患者
    2020-12-01 11:46

    If I were implement my own language and want to support arbitrary length numbers, I will use a target language with the carry/borrow concept. But since there is no HLL that implements this without severe performance implications (like exceptions), I will certainly go implement it in assembly. It will probably take a single instruction (as in JC in x86) to check for overflow and handle it (as in ADC in x86), which is an acceptable compromise for a language implementing arbitrary precision. Then I will use a few functions written in assembly instead of regular operators, if you can utilize overloading for a more elegant output, even better. But I don't expect generated C++ to be maintainable (or meant to be maintained) as a target language.

    Or, just use a library which has more bells and whistles than you need and use it for all your numbers.

    As a hybrid approach, detect overflow in assembly and call the library function if overflow instead of rolling your own mini library.

提交回复
热议问题