Radix (base) conversion

2010-07-23 Thread Jan Ingvoldstad
Hi.

I was fiddling about with a small example of how nice radix adverbials are
for conversion:

my $x = 6*9;
say :13($x);

rakudo: 69

($x = 54 in base 10, but 54 in base 13 is 69 in base 10.)

Strangely enough, I cannot find a way — in the spec — of both treating a
number as something in base 13 as well as displaying it in base 13.

sprintf() has formats for binary, octal and hexadecimal, but there appears
no way to use an arbitrary base.

As a clarification, see this example form bc(1):

obase=13
print What do you get when you multiply six by nine? ; 6*9
What do you get when you multiply six by nine? 42
obase=10


Am I missing something?


It is also somewhat confusing that while $x stores the result of the
multiplication of 6*9, the adverbial radix conversion treats the variable as
a literal and no longer a value.
-- 
Jan


Re: Radix (base) conversion

2010-07-23 Thread jerry gay
On Fri, Jul 23, 2010 at 05:17, Jan Ingvoldstad frett...@gmail.com wrote:
 Hi.

 I was fiddling about with a small example of how nice radix adverbials are
 for conversion:

 my $x = 6*9;
 say :13($x);

 rakudo: 69

 ($x = 54 in base 10, but 54 in base 13 is 69 in base 10.)

 Strangely enough, I cannot find a way — in the spec — of both treating a
 number as something in base 13 as well as displaying it in base 13.

 sprintf() has formats for binary, octal and hexadecimal, but there appears
 no way to use an arbitrary base.

 As a clarification, see this example form bc(1):

 obase=13
 print What do you get when you multiply six by nine? ; 6*9
 What do you get when you multiply six by nine? 42
 obase=10


 Am I missing something?


 It is also somewhat confusing that while $x stores the result of the
 multiplication of 6*9, the adverbial radix conversion treats the variable as
 a literal and no longer a value.
 --
 Jan

perhaps a Rat should be displayed, with the base as denominator?  say
:13(6 * 9);   # 42/13

~jerry


Re: Radix (base) conversion

2010-07-23 Thread Mark J. Reed
No, 42/13 is 42 over 13, which is 3 + 3/13.  Let's not confuse
fractions and bases, please.

:13(42) means 54.  That much is straightforward.

:13(6*9) is less obvious, but seems to mean (1) multiply 6*9,  (2)
convert the resulting number to a (decimal) string; (3) interpret that
string in base 13.  Hence :13(54) or decimal 69.

Either way, AIUI, the radix is only used to control the interpretation
of the literal.  The result is just a number, which doesn't know
that it was entered in base 13.  It's just a value, without any
inherent notion of a particular radix.

Or at least I thought that was the case, but in current Rakudo I
notice that :13(54) is a Num while 69 is an Int.

--
Mark J. Reed markjr...@gmail.com


Re: Radix (base) conversion

2010-07-23 Thread jerry gay
On Fri, Jul 23, 2010 at 07:45, Mark J. Reed markjr...@gmail.com wrote:
 No, 42/13 is 42 over 13, which is 3 + 3/13.  Let's not confuse
 fractions and bases, please.

ha! yet another case of crossed wires too early in the morning.  sorry
for the confusion, i've been making similar apologies all day.  too
bad i don't drink coffee

~jerry