Exponentiation with infix operators in (or operating on) the base: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Use wiki markup for links) |
(→{{header|Smalltalk}}: added Sorry for the output to look different; but it does not really make sense here) |
||
Line 316: | Line 316: | ||
+5**3 == 125; -+5**3 ==-125; -(+5)**3 ==-125; (-+5)**3 ==-125; -(+5**3) ==-125</pre> |
+5**3 == 125; -+5**3 ==-125; -(+5)**3 ==-125; (-+5)**3 ==-125; -(+5**3) ==-125</pre> |
||
=={{header|Smalltalk}}== |
|||
Smalltalk has no prefix operator for negation. To negate, you have to send the number a "negated" message, which has higher precedence than any binary message. Literal constants may have a sign (which is not an operation, but part of the constant). |
|||
<lang smalltalk> |
|||
a := 2. |
|||
b := 3. |
|||
Transcript show:'-5**2 => '; showCR: -5**2. |
|||
Transcript show:'-5**a => '; showCR: -5**a. |
|||
Transcript show:'-5**b => '; showCR: -5**b. |
|||
Transcript show:'5**2 => '; showCR: 5**2. |
|||
Transcript show:'5**a => '; showCR: 5**a. |
|||
Transcript show:'5**b => '; showCR: 5**b. |
|||
" Transcript show:'-(5**b) => '; showCR: -(5**b). -- invalid: syntax error " |
|||
" Transcript show:'5**-b => '; showCR: 5**-b. -- invalid: syntax error "</lang> |
|||
Using the "negated" message: |
|||
<lang smalltalk> |
|||
Transcript show:'5 negated**2 => '; showCR: 5 negated**2. "negates 5" |
|||
Transcript show:'5 negated**3 => '; showCR: 5 negated**3. |
|||
Transcript show:'5**2 negated => '; showCR: 5**2 negated. "negates 2" |
|||
Transcript show:'5**3 negated => '; showCR: 5**3 negated. "negates 3" |
|||
Transcript show:'5 negated**a => '; showCR: 5 negated**a. |
|||
Transcript show:'5 negated**b => '; showCR: 5 negated**b. |
|||
Transcript show:'5**a negated => '; showCR: 5**a negated. |
|||
Transcript show:'5**b negated => '; showCR: 5**b negated. |
|||
Transcript show:'(5**a) negated => '; showCR: (5**a) negated. |
|||
Transcript show:'(5**b) negated => '; showCR: (5**b) negated. |
|||
Transcript show:'(-5**a) negated => '; showCR: (-5**a) negated. |
|||
Transcript show:'(-5**b) negated => '; showCR: (-5**b) negated. |
|||
Transcript show:'-5 negated**2 => '; showCR: -5 negated**2. "negates the negative 5" |
|||
Transcript show:'-5 negated**3 => '; showCR: -5 negated**3. |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
-5**2 => 25 |
|||
-5**a => 25 |
|||
-5**b => -125 |
|||
5**2 => 25 |
|||
5**a => 25 |
|||
5**b => 125 |
|||
5 negated**2 => 25 |
|||
5 negated**3 => -125 |
|||
5**2 negated => (1/25) |
|||
5**3 negated => (1/125) |
|||
5 negated**a => 25 |
|||
5 negated**b => -125 |
|||
5**a negated => (1/25) |
|||
5**b negated => (1/125) |
|||
(5**a) negated => -25 |
|||
(5**b) negated => -125 |
|||
(-5**a) negated => -25 |
|||
(-5**b) negated => 125 |
|||
-5 negated**2 => 25 |
|||
-5 negated**3 => 125 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |