Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
manual:chapter3:reals [2019/06/24 13:18]
claudio [Bit operations]
manual:chapter3:reals [2019/11/06 15:21] (current)
jojo1973 Added Special symbols
Line 1: Line 1:
 ===== Basic numbers ===== ===== Basic numbers =====
  
-----+==== Approximate versus Exact ====
  
-==== Approximate versus Exact ==== +A number in **newRPL** is represented as exact or approximate. ​ To enter a number as approximate use a trailing dot.  To enter a number as exact omit any trailing dot.  For routine arithmetic this distinction doesn'​t matter much, but it does come into play when evaluating symbolic expressions.
-A number in newRPL is represented as exact or approximate. ​ To enter a number as approximate use a trailing dot.  To enter a number as exact omit any trailing dot.  For routine arithmetic this distinction doesn'​t matter much, but it does come into play when evaluating symbolic expressions.+
  
 Here are some examples: Here are some examples:
  
-| **Approximate Numbers** | **Exact Numbers** | +Approximate Numbers ​Exact Numbers ​^ 
-| 1. | 1 | +''​1.'' ​''​1'' ​
-| 1.007. | 1.007 | +''​1.007.'' ​''​1.007'' ​
-| 1.007.e-10 | 1.007e-10 |+''​1.007.e-10'' ​''​1.007e-10'' ​|
  
 Arithmetic performed on numbers takes into account the operands (exact or approximate) and results are displayed accordingly. Arithmetic performed on numbers takes into account the operands (exact or approximate) and results are displayed accordingly.
  
-For example, ​''​1 3 /'' ​results in ''​0.333.''​ (approximate),​ whereas ​''​1 2 /'' ​results in ''​0.5''​ (exact).+For example, ​<​code>​ 
 +2:                             1 
 +1:                             3 
 +…………………………………………………………………………………… 
 +/ 
 +</​code> ​results in ''​0.333333333.''​ (approximate),​ whereas ​<​code>​ 
 +2:                             1 
 +1:                             2 
 +…………………………………………………………………………………… 
 +/ 
 +</​code> ​results in ''​0.5''​ (exact).
  
----- +----
  
 ==== Numbers in other bases ==== ==== Numbers in other bases ====
  
-Numbers in different bases can be entered by preceeding ​the value with a ''#''​ and a trailing letter to indicate the base (''​b''​ = binary, ''​o''​ = octal, ''​d''​ = decimal, ''​h''​ = hex, note that the trailing letter is case insensitive).  Arithmetic can be done on numbers in different bases with the result displayed as the base of the first argument. ​ Only exact numbers in the range $-2^{63}$ to $2^{63}-1$ can be expressed in multiple bases. ​ Outside this range will be switched to decimal.+Numbers in different bases can be entered by preceding ​the value with a ''#''​ and a trailing ​letter to indicate the base (''​b''​ = binary, ''​o''​ = octal, ''​d''​ = decimal, ''​h''​ = hexadecimal; ​note that the trailing letter is case insensitive ​and that even if the ''#​d''​ syntax for decimal ​numbers is accepted, they are nevertheless displayed without leading ''#''​ and trailing ''​d''​).
  
-Here are example arithmetic operations ​in bases other than 10:+A quicker method to enter these numbers consists ​in using the **base cycler**simply start keying the number in the preferred base then press **RS<​sup>​hold</​sup>​-3** and the number on the edit line will cycle between the different bases.
  
-''#​1101b #FFh +'' ​yields ​''​100001100b''​+The base cycler is smart enough to skip bases if the keyed number contains illegal digits. For example, if there'​s ''​123''​ on the edit line the base cycler will rotate between ​''#​123o'',​ ''​#123h'' ​and ''​123'',​ skipping an illegal ''#​123b''​.
  
-''​256 #FFFFh +''​ yields ''​65,791''​+If the number is already on the stack the base cycler will display all four bases in successionprovided the number is integer.
  
-''#​355o #11010101b x''​ yields ''#​142461o''​+Another option is the BASE menu (**RS-3**): in its first page the soft key will give access to the aforementioned base cycler and four __non-programmable__ base conversion tools which act on the number on the edit line or the first level of the stack.
  
-''#​7h ​/''​ yields ''​3.5''​+Arithmetic can be done on numbers in different bases with the result displayed as the base of the first argument. ​ Only integer numbers in the range $-(2^{63}-1)$ to $2^{63}-1$ can be expressed in multiple bases. Numbers can be either exact or approximate;​ outside this range the base will be automatically switched to decimal.
  
-''​#2h 63 Y^X'' ​yields ​''​9.223372E18''​+In other words, it is assumed that **non base-10 numbers are signed, 64-bits integers**. Of these 64 bits, the least significant 63 bits store the magnitude and the 64th most significant bit stores the sign (''​0'' ​means positive, ​''​1'' ​means negative). Negative numbers are stored internally in __two'​s complement__ but are shown as positive numbers with a leading minus sign.
  
-----+For example: <​code>​ 
 +2:                         #​125o 
 +1:                          #EEh 
 +…………………………………………………………………………………… 
 +- 
 +</​code>​ yields ''​-#​231o''​.
  
-==== Bit operations ​====+Here are more examples of arithmetic ​operations ​in bases other than 10:
  
-| **Command** | **Function** | **Example** | +<​code>​ 
-|BADD|Add|''​#11001b #100000b BADD''​ yields ''#​111001b''​| +2:                        ​#1101b 
-|BSUB|Subtract| ''​#11001b #100000b BSUB''​ yields ''​-#​111b''​ | +1:                          ​#FFh 
-|BMUL|Multiply| ''#​11001b #100000b BMUL''​ yields ''#​1100100000b''​ | +…………………………………………………………………………………… 
-|BDIV|Divide| ''#​11001b #100000b BDIV''​ yields ''#​0b''​ | ++ 
-|BAND|Logical AND| ''#​1101010b #1100010b BAND''​ yields ''#​1100010b''​ | +</​code> ​yields ''#​100001100b''​.
-|BOR|Logical OR| ''#​1101010b #1100010b BOR''​ yields ''#​1101010b''​ | +
-|BXOR|Logical XOR| ''#​1101010b #1100010b BXOR'' ​yields ''#​1000b'' ​|+
  
-Note that ''​BMUL'' ​and ''​BDIV''​ can be used to left shift and right shift numbers (respectively) when the second operand is a power of 2.+<​code>​ 
 +2:                           256 
 +1:                        #FFFFh 
 +…………………………………………………………………………………… 
 +
 +</​code>​ yields ​''​-65279''​.
  
-----+<​code>​ 
 +2:                         #​355o 
 +1:                    #​11010101b 
 +…………………………………………………………………………………… 
 +
 +</​code>​ yields ''#​142461o''​. 
 + 
 +<​code>​ 
 +2:                           #7h 
 +1:                             2 
 +…………………………………………………………………………………… 
 +
 +</​code>​ yields ''​3.5''​. 
 + 
 +<​code>​ 
 +2:                           #2h 
 +1:                            63 
 +…………………………………………………………………………………… 
 +
 +</​code>​ yields ''​9.223372E18''​.
  
 ==== Setting the word size ==== ==== Setting the word size ====
  
-The word size, applicable to exact numbers in other bases, ​can be set using the command ''​STWS''​ (STore Word Size). ​ Valid ranges are 1 to 63 (not including the sign bit).  So, for example, to work with 32 bit signed ​numbers, set the word size to 31.  To view the currently set word size, use ''​RCWS''​ (ReCall Word Size).  ​//Note that setting too small a word size can lead to "​rolling the register" ​and hence unexpected results, such as this://+If there is a need to work with different word sizes **newRPL** provides a dedicated set of **bit operations**:​ these command always honor the selected word size //when returning the result//. 
 + 
 +The word size can be set using the command ''​[[manual:​chapter6:​binary:​cmd_stws|STWS]]''​ (STore Word Size). ​ Valid ranges are 1 to 63 (not including the sign bit).  So, for example, to work with __32-bit signed ​numbers__, set the word size to ''​31''​. To view the currently set word size, use ''​[[manual:​chapter6:​binary:​cmd_rcws|RCWS]]''​ (ReCall Word Size). ​ /Note that setting too small a word size can lead to overflow ​and hence unexpected results, such as this:
  
 <​code>​ <​code>​
-7 STWS+« 7 STWS 
 +  120 4 BMUL 
 +» 
 +</​code>​
  
-120 4 BMUL+The result is ''​-32'',​ not ''​480''​ as expected with a larger word size.
  
--32+==== Bit operations ==== 
 + 
 +^ Command ^ Purpose ^ Example ^ 
 +| ''​[[manual:​chapter6:​binary:​cmd_badd|BADD]]''​ | Add | ''#​11001b #100000b [[manual:​chapter6:​binary:​cmd_badd|BADD]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bsub|BSUB]]''​ | Subtract | ''#​371o #250 [[manual:​chapter6:​binary:​cmd_bsub|BSUB]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bmul|BMUL]]''​ | Multiply | ''#​1A4h #7h [[manual:​chapter6:​binary:​cmd_bmul|BMUL]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bdiv|BDIV]]''​ | Divide | ''​12 5 [[manual:​chapter6:​binary:​cmd_bdiv|BDIV]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bneg|BNEG]]''​ | Negate (Two's Complement) | ''#​11001b [[manual:​chapter6:​binary:​cmd_bneg|BNEG]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_band|BAND]]''​ | Bitwise AND | ''#​1101010b #1100010b [[manual:​chapter6:​binary:​cmd_band|BAND]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bor|BOR]]''​ | Bitwise OR | ''#​1101010b #1100010b [[manual:​chapter6:​binary:​cmd_bor|BOR]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bxor|BXOR]]''​ | Bitwise XOR | ''#​1101010b #1100010b [[manual:​chapter6:​binary:​cmd_bxor|BXOR]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_bnot|BNOT]]''​ | Bitwise NOT (One's Complement) | ''#​11001b [[manual:​chapter6:​binary:​cmd_bnot|BNOT]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_blsl|BLSL]]''​ | Bitwise Logical Shift Left | ''#​11001b 4 [[manual:​chapter6:​binary:​cmd_blsl|BLSL]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_blsr|BLSR]]''​ | Bitwise Logical Shift Right | ''#​11001b 2 [[manual:​chapter6:​binary:​cmd_blsr|BLSR]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_basr|BASR]]''​ | Bitwise Arithmetic Shift Right | ''#​215o 2 [[manual:​chapter6:​binary:​cmd_basr|BASR]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_brl|BRL]]''​ | Bitwise Rotate Left | ''#​3FFh 4 [[manual:​chapter6:​binary:​cmd_brl|BRL]]''​ | 
 +| ''​[[manual:​chapter6:​binary:​cmd_brr|BRR]]''​ | Bitwise Rotate Right | ''#​3FFh 1 [[manual:​chapter6:​binary:​cmd_brr|BRR]]''​ | 
 + 
 +---- 
 + 
 +==== Testing ==== 
 + 
 +Real numbers form an ordered field and its elements can be compared: **newRPL** provides a full set of arithmetic and logic operators plus some specialized ones. 
 + 
 +All these operators (except ''​[[manual:​chapter6:​operators:​cmd_ovr_cmp|CMP]]''​) return ''​1''​ __if the test is true__ and ''​0''​ __if the test is false__. Actually any value different from ''​0''​ is considered __true__, but conventionally the operators return always ''​1''​ when the test succeeds. 
 + 
 +The classical **arithmetic** test operators are ''​[[manual:​chapter6:​operators:​cmd_ovr_eq|==]]''​ (equality), ''​[[manual:​chapter6:​operators:​cmd_ovr_noteq|≠]]''​ (not equality), ''​[[manual:​chapter6:​operators:​cmd_ovr_lt|<​]]''​ (less than), ''​[[manual:​chapter6:​operators:​cmd_ovr_gt|>​]]''​ (greater than), ''​[[manual:​chapter6:​operators:​cmd_ovr_lte|≤]]''​ (less than or equal), ''​[[manual:​chapter6:​operators:​cmd_ovr_gte|≥]]''​ (greater than or equal). 
 + 
 +<​code>​ 
 +2:                          #10h 
 +1:                       #​10000b 
 +…………………………………………………………………………………… 
 +== 
 +</​code>​ yields ''​1''​. 
 + 
 +<​code>​ 
 +2:                          #11o 
 +1:                           10. 
 +…………………………………………………………………………………… 
 +
 +</​code>​ yields ''​0''​. 
 + 
 +**Logical** test operators are ''​[[[manual:​chapter6:​operators:​cmd_ovr_and|AND]]'',​ ''​[[[manual:​chapter6:​operators:​cmd_ovr_or|OR]]'',​ ''​[[[manual:​chapter6:​operators:​cmd_ovr_xor|XOR]]''​ and ''​[[[manual:​chapter6:​operators:​cmd_ovr_not|NOT]]''​. 
 + 
 +These operators must not be confused with the similar named bitwise operators ''​[[manual:​chapter6:​binary:​cmd_band|BAND]]'',​ ''​[[manual:​chapter6:​binary:​cmd_bor|BOR]]'',​ ''​[[manual:​chapter6:​binary:​cmd_bxor|BXOR]]''​ and ''​[[manual:​chapter6:​binary:​cmd_bnot|BNOT]]''​ described earlier: while the latter operate on a bit-by-bit basis, the logical operators consider the //truth value// of their arguments. Some examples will help to clarify: 
 + 
 +<​code>​ 
 +2:                        #1000b 
 +1:                       #​11000b 
 +…………………………………………………………………………………… 
 +BAND
 </​code>​ </​code>​
 +yields ''#​1000b'',​ but
 +<​code>​
 +2:                        #1000b
 +1:                       #​11000b
 +……………………………………………………………………………………
 +AND
 +</​code>​
 +yields ''​1''​. Similarly
 +<​code>​
 +2:                        #1000b
 +1:                           #0b
 +……………………………………………………………………………………
 +BOR
 +</​code>​
 +yields ''#​1000b'',​ but
 +<​code>​
 +2:                        #1000b
 +1:                           #0b
 +……………………………………………………………………………………
 +AND
 +</​code>​
 +yields ''​0''​.
  
-The result is -32not the result ​of 480 as expected with a larger word size.+Finally**specialized** test operators perform particular tests: they are ''​[[manual:​chapter6:​operators:​cmd_ovr_cmp|CMP]]'',​ ''​[[manual:​chapter6:​operators:​cmd_ovr_same|SAME]]''​ and ''​[[manual:​chapter6:​operators:​cmd_ovr_istrue|ISTRUE]]''​. 
 + 
 +''​[[manual:​chapter6:​operators:​cmd_ovr_cmp|CMP]]''​ returns ​the ''​[[manual:​chapter6:​arithmetic:​cmd_ovr_sign|SIGN]]'' ​of the difference between its arguments:​ 
 +<​code>​ 
 +2:                            -2 
 +1:                             8 
 +…………………………………………………………………………………… 
 +CMP 
 +</​code>​ 
 +yields ''​-1''​. 
 + 
 +For real numbers  
 +  * ''​[[manual:​chapter6:​operators:​cmd_ovr_same|SAME]]''​ and ''​[[manual:​chapter6:​operators:​cmd_ovr_eq|==]]''​ are equivalent;​ 
 +  * ''​[[manual:​chapter6:​operators:​cmd_ovr_istrue|ISTRUE]]''​ and ''​« 0 [[manual:​chapter6:​operators:​cmd_ovr_noteq|≠]] »''​ are equivalent.
  
 ---- ----
 +  ​
 +==== Special symbols ====
 +
 +Operations on real numbers aren't always defined on the whole field of real numbers: a notable example is the division, which is not defined if the divisor is ''​0''​. **newRPL** handles these occurrances defining two special symbols: ''​∞''​ (//​infinity//​) and ''​NaN''​ (//​Not-a-Number//​).
 +
 +The ''​∞''​ symbol is returned when attempting, as anticipated,​ a division by ''​0''​ or when the result of a function (e.g. ''​[[manual:​chapter6:​transcendentals:​cmd_tan|TAN]]''​) diverges. Albeit ''​∞''​ must not be considered a number, it obeys to a number of rules. The following examples assume flag [[manual:​appendix:​flags#​flag-103|-103]] is cleared.
 +
 +  * ''​∞''​ ''​[[manual:​chapter6:​operators:​cmd_ovr_add|+]]''​ or ''​[[manual:​chapter6:​operators:​cmd_ovr_sub|-]]''​ a finite number yields ''​∞'';​
 +  * ''​∞''​ ''​[[manual:​chapter6:​operators:​cmd_ovr_mul|*]]''​ or ''​[[manual:​chapter6:​operators:​cmd_ovr_div|/​]]''​ a finite number yields ''​∞'';​
 +  * the sign rule in multiplication or division applies, therefore ''​-∞''​ is a valid object;
 +  * sum, multiplication or power of ''​∞''​ with itself yields ''​∞''​.
 +  ​
 +The ''​NaN''​ symbol is **newRPL** way of informing the user that the attempted calculation returns an //​undefined//​ result: typical examples include the calculation of the difference ''​∞ - ∞''​ or the sine of ''​∞''​. In general any function that includes ''​NaN''​ among its arguments will yield ''​NaN''​.
  • manual/chapter3/reals.1561407511.txt.gz
  • Last modified: 2019/06/24 13:18
  • by claudio