Octal multiplication

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Octal multiplication

nacho
Hello,
This could sound like a dumb question, it may be.
If I print:
8r3 * 8r3 I get 9.
It seems that Pharo converts first to decimal and then performs the multiplication. How do I get the result in octal which should be 8r11?
Thanks!
Nacho Smalltalker apprentice. Buenos Aires, Argentina.
Reply | Threaded
Open this post in threaded view
|

Re: Octal multiplication

Peter Uhnak
On Sun, Apr 23, 2017 at 06:13:42AM -0700, nacho wrote:
> Hello,
> This could sound like a dumb question, it may be.
> If I print:
> 8r3 * 8r3 I get 9.
> It seems that Pharo converts first to decimal and then performs the
> multiplication. How do I get the result in octal which should be 8r11?


#printStringRadix: base

(8r3 * 8r3) printStringRadix: 8 "-> 8r11"

for hex there's also

#hex

12 hex "-> 16rC"

Peter


> Thanks!
>
>
>
>
> -----
> Nacho
> Smalltalker apprentice.
> Buenos Aires, Argentina.
> --
> View this message in context: http://forum.world.st/Octal-multiplication-tp4943364.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Octal multiplication

raffaello.giulietti
In reply to this post by nacho
On 23/04/17 15:13, nacho wrote:
> Hello,
> This could sound like a dumb question, it may be.
> If I print:
> 8r3 * 8r3 I get 9.
> It seems that Pharo converts first to decimal and then performs the
> multiplication. How do I get the result in octal which should be 8r11?
> Thanks!
>

Hi Nacho

There is no such thing as an "octal multiplication".

Smalltalk has a notion of numerical literals, like 28, 10r28, 8r34 or
16r1C. They all represent the same number, namely the abstract notion of
twenty-eight. But it not correct to say that Smalltalk first converts to
decimal. The internal representation of an integer value in Smalltalk
is, in fact, binary, but this should be mostly transparent to most
usages. Smalltalk code does not care whether twenty-eight appears as
octal or whatever in source code.

Something similar holds in C, C++, Java, Lisp, Python, Haskell or any
other of thousands of programming languages out there. If you are used
to one of these, you should not be surprised by the behavior of Smalltalk.

When a numerical result is printed, it is usually written as decimal
number by default (in Smalltalk as well as in many other languages). As
Peter points out, you must be explicit if you need a different
representation.

Greetings
Raffaello