Mercurial > octave
comparison doc/interpreter/numbers.txi @ 31069:43974344fe19 stable
doc: Expand documentation about type promotion and demotion (bug #62283)
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Sat, 04 Jun 2022 17:13:38 -0400 |
parents | 796f54d4ddbf |
children | 2dee06f4635c |
comparison
equal
deleted
inserted
replaced
31065:d33af4594440 | 31069:43974344fe19 |
---|---|
773 @end example | 773 @end example |
774 | 774 |
775 @noindent | 775 @noindent |
776 where the returned value is single precision. | 776 where the returned value is single precision. |
777 | 777 |
778 Many functions and operators will also promote integer or logical types to | |
779 double, or single to double, especially if they take only one argument. | |
780 | |
781 @example | |
782 @group | |
783 a = det (int8 ([1 2; 3 4])) | |
784 @result{} a = -2 | |
785 class (a) | |
786 @result{} double | |
787 @end group | |
788 @end example | |
789 | |
790 But there are also exceptions for promoting to double, especially if the | |
791 function or operator in question can take multiple arguments. | |
792 | |
793 @example | |
794 @group | |
795 a = eig (int8 ([1 2; 3 4])) | |
796 @result{} error: eig: wrong type argument 'int8 matrix' | |
797 @end group | |
798 @end example | |
799 | |
800 When the two operands are both integers but of different widths, then the | |
801 behavior depends on the operator or the function in question. For some | |
802 operators and functions, narrow-bitwidth operands are promoted to a wider | |
803 bitwidth: | |
804 | |
805 @example | |
806 @group | |
807 a = min (int8 (100), int16 (200)) | |
808 @result{} 100 | |
809 class (a) | |
810 @result{} int16 | |
811 @end group | |
812 @end example | |
813 | |
814 However, not all functions or operators will accept integer operands of | |
815 differing types: | |
816 | |
817 @example | |
818 @group | |
819 int8 (100) + int16 (200) | |
820 @result{} error: binary operator '+' not implemented | |
821 for 'int8 scalar' by 'int16 scalar' operations | |
822 @end group | |
823 @end example | |
824 | |
825 Further, in most cases, both operands need to be signed or both need to be | |
826 unsigned. Mixing signed and unsigned usually causes an error, even if they | |
827 are of the same bitwidth. | |
828 | |
829 @example | |
830 @group | |
831 min (int8 (100), uint16 (200)) | |
832 @result{} error: min: cannot compute min (int8 scalar, uint16 scalar) | |
833 @end group | |
834 @end example | |
835 | |
778 In the case of mixed type indexed assignments, the type is not | 836 In the case of mixed type indexed assignments, the type is not |
779 changed. For example, | 837 changed. For example, |
780 | 838 |
781 @example | 839 @example |
782 @group | 840 @group |