comparison liboctave/oct-inttypes.h @ 10436:00219bdd2d17

implement built-in rem and mod
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 23 Mar 2010 13:01:34 +0100
parents cc69a17ec801
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
10435:6a271334750c 10436:00219bdd2d17
384 else 384 else
385 { 385 {
386 return x ? octave_int_base<T>::max_val () : 0; 386 return x ? octave_int_base<T>::max_val () : 0;
387 } 387 }
388 } 388 }
389
390 // Remainder.
391 static T
392 rem (T x, T y)
393 {
394 return y != 0 ? x % y : 0;
395 }
396
397 // Modulus. Note the weird y = 0 case for Matlab compatibility.
398 static T
399 mod (T x, T y)
400 {
401 return y != 0 ? x % y : x;
402 }
389 }; 403 };
390 404
391 #ifdef OCTAVE_INT_USE_LONG_DOUBLE 405 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
392 // Handle 64-bit multiply using long double 406 // Handle 64-bit multiply using long double
393 template <> 407 template <>
652 z += 1 - (signbit (x) << 1); 666 z += 1 - (signbit (x) << 1);
653 } 667 }
654 return z; 668 return z;
655 } 669 }
656 670
671 // Remainder.
672 static T
673 rem (T x, T y)
674 {
675 return y != 0 ? x % y : 0;
676 }
677
678 // Modulus. Note the weird y = 0 case for Matlab compatibility.
679 static T
680 mod (T x, T y)
681 {
682 if (y != 0)
683 {
684 T r = x % y;
685 return ((r < 0) != (y < 0)) ? r + y : r;
686 }
687 else
688 return x;
689 }
657 }; 690 };
658 691
659 #ifdef OCTAVE_INT_USE_LONG_DOUBLE 692 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
660 // Handle 64-bit multiply using long double 693 // Handle 64-bit multiply using long double
661 template <> 694 template <>
779 812
780 OCTAVE_INT_BIN_OP(+, add, octave_int<T>) 813 OCTAVE_INT_BIN_OP(+, add, octave_int<T>)
781 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>) 814 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>)
782 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>) 815 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>)
783 OCTAVE_INT_BIN_OP(/, div, octave_int<T>) 816 OCTAVE_INT_BIN_OP(/, div, octave_int<T>)
817 OCTAVE_INT_BIN_OP(%, rem, octave_int<T>)
784 OCTAVE_INT_BIN_OP(<<, lshift, int) 818 OCTAVE_INT_BIN_OP(<<, lshift, int)
785 OCTAVE_INT_BIN_OP(>>, rshift, int) 819 OCTAVE_INT_BIN_OP(>>, rshift, int)
786 820
787 #undef OCTAVE_INT_BIN_OP 821 #undef OCTAVE_INT_BIN_OP
788 822
804 838
805 private: 839 private:
806 840
807 T ival; 841 T ival;
808 }; 842 };
843
844 template <class T>
845 inline octave_int<T>
846 rem (const octave_int<T>& x, const octave_int<T>& y)
847 { return octave_int_arith<T>::rem (x.value (), y.value ()); }
848
849 template <class T>
850 inline octave_int<T>
851 mod (const octave_int<T>& x, const octave_int<T>& y)
852 { return octave_int_arith<T>::mod (x.value (), y.value ()); }
809 853
810 // No mixed integer binary operations! 854 // No mixed integer binary operations!
811 855
812 template <class T> 856 template <class T>
813 inline bool 857 inline bool