Modular arithmetic: Difference between revisions

Line 83:
=={{header|ATS}}==
 
The following program uses the <code>unsigned __int128</code> type of GNU C. It could easily be modified, however, not to use that. I use the compiler extension to implement modular multiplication that, works for all moduli possible with the ordinary integer types on AMD64, does not overflow.
 
And I wouldlet be"modulus tempted0" mean to saydo thatordinary supportingunsigned modulusarithmetic, 2**(wordsize)so amountsI might be tempted to say the code is transparently supporting both modular and non-modular integers. It is, after all, the normal arithmetic of the language. However, 10**100 would overflow the register, so the result would ''actually'' be modulo 2**(wordsize). There is, in fact, with C semantics for unsigned integers, no way to do non-modular arithmetic with unsigned integers of fixed size! You automatically get 2**(wordsize) as a modulus. And you cannot safely use signed integers at all.
 
(''Aside: A definition of f(x) that worked with signed integers, floating point, etc., as well, would have to be a macro. This is possible because of the overloading of the operators. Alternatively, you could introduce a'' <code>datatype</code> ''for "numbers of different kinds", and write f(x) to work with that less efficient type.'')
1,448

edits