Re: Dual Buf and Str question

2022-06-11 Thread ToddAndMargo via perl6-users

On 6/9/22 22:54, ToddAndMargo via perl6-users wrote:

Hi All,

I can easily  get away with this in Modula2, but
how can I do this with Raku?

I wish to create a single variable that can be
manipulated in two ways:

1) as a fixed length string (Str)

2) as a fixed length buffer (Buf)

I can think of ways to do this, but it would
require separate variable and conversions routines
back and forth.

Any words of Raku wisdom?

Many thanks,
-T




Hi All,

I do believe what I am asking is not possible in
Raku.  In Modula2, it is ridiculously easy to
do. But there is a fly in the ointment.  There
is a possibility of chr(0)'s in the resultant
string.  And in Modula2, as is in C, that is
a string terminator.  So Raku is a much better
choice, as the length of a string is kept in
a hidden structure and not in the the string
itself.  And I am done with Modula2 and use
Raku for a reason.

Anyway, I have been wrapping my mind around
how to do this in Raku.  With some chr's and
ord's, I can accomplish what I need.  And
some string tricks I learned in Perl 5.

When I come up working module for this, I
will get back.  I think you all will find it
very useful.  Ya, I know I am being
a bit cryptic, but all will be revealed.
Eventually.

:-)

-T


BigRoot precision question

2022-06-11 Thread ToddAndMargo via perl6-users

Hi All,

In the following paper on Big Root:

https://newbedev.com/how-can-i-set-the-level-of-precision-for-raku-s-sqrt


> use BigRoot;
> BigRoot.precision = 7;
> say (BigRoot.newton's-sqrt: 2;).base(10)
1.4142136
> say (BigRoot.newton's-sqrt: 2;).base(16)
1.6A09E7

That is a base(10) precision.  In base(16) (Hex)
that is only 3 digits past the decimal.
0x6A-0x09-0xE7

Is there a way to tell BigRoot I want a certain
amount of Hex digits I want after the decimal?

Or just ask for double I want and prune afterwards?


Many thanks,
-T



--
~~
When you say, "I wrote a program that
crashed Windows," people just stare at
you blankly and say, "Hey, I got those
with the system, for free."
 -- Linus Torvalds
~~


Re: Easier way to load a buffer?

2022-06-11 Thread ToddAndMargo via perl6-users

On 6/10/22 07:49, ToddAndMargo via perl6-users wrote:

Hi All,

I am looking for an easier way to load a buffer.

I know about this way

[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
Buf:0x<2A 54 FF 53>

I would like to do it on one big blast:

my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
Cannot unbox 170 bit wide bigint into native integer

But do not know the proper syntax.

Any words of wisdom?  Am I stuck with the hard way?

Many thanks,
-T




Came up with a Raku way of doing what I want.
And dropping the need for Buf at the same time:


> use BigRoot;
> BigRoot.precision = 40;
40

> my $root5 = BigRoot.newton's-sqrt: 5;
> $y = sprintf  $root5.base(16)
2.3C6EF372FE94F82BE73980C0B9DB90681F

> $y ~~ s/ $( Q[.] ) //;
「.」

> say $y
23C6EF372FE94F82BE73980C0B9DB90681F

Add a loop and and some chr's and ord's and
happy camping will proceed!

:-)

-T


Re: BigRoot precision question

2022-06-11 Thread Bruce Gray



> On Jun 11, 2022, at 4:41 AM, ToddAndMargo via perl6-users 
>  wrote:
> 
> Hi All,
> 
> In the following paper on Big Root:
> 
> https://newbedev.com/how-can-i-set-the-level-of-precision-for-raku-s-sqrt
> 
> 
> > use BigRoot;
> > BigRoot.precision = 7;
> > say (BigRoot.newton's-sqrt: 2;).base(10)
> 1.4142136
> > say (BigRoot.newton's-sqrt: 2;).base(16)
> 1.6A09E7
> 
> That is a base(10) precision.  In base(16) (Hex)
> that is only 3 digits past the decimal.
>0x6A-0x09-0xE7
> 
> Is there a way to tell BigRoot I want a certain
> amount of Hex digits I want after the decimal?

No, https://github.com/juliodcs/BigRoot/blob/master/lib/BigRoot.rakumod shows 
that BigRoot only allows setting precision as decimal.

> Or just ask for double I want and prune afterwards?

Doubling your desired precision is overkill; you only need a 21% increase. 
Since:
decimal_precision =~= ( hexadecimal_precision_wanted * log(16) / log(10) );
, this should work:
BigRoot.precision = ( $hexadecimal_precision_wanted * log10(16) ).ceiling;

As evidence:
my $hexadecimal_precision_wanted = 2 ** 13;
my $decimal_precision_via_log= ( $hexadecimal_precision_wanted * 
log10(16) ).ceiling;
my $largest_hex_number_that_size = 'F' x $hexadecimal_precision_wanted;
my $decimal_precision_via_chars  = 
$largest_hex_number_that_size.parse-base(16).chars;
.say for ( :$hexadecimal_precision_wanted, :$decimal_precision_via_log, 
:$decimal_precision_via_chars );
Output:
hexadecimal_precision_wanted => 8192
decimal_precision_via_log => 9865
decimal_precision_via_chars => 9865

-- 
Hope this helps,
Bruce Gray (Util of PerlMonks) [who just learned of the BigRoot module]



Re: Dual Buf and Str question

2022-06-11 Thread ToddAndMargo via perl6-users

On 6/11/22 02:28, ToddAndMargo via perl6-users wrote:

On 6/9/22 22:54, ToddAndMargo via perl6-users wrote:

Hi All,

I can easily  get away with this in Modula2, but
how can I do this with Raku?

I wish to create a single variable that can be
manipulated in two ways:

1) as a fixed length string (Str)

2) as a fixed length buffer (Buf)

I can think of ways to do this, but it would
require separate variable and conversions routines
back and forth.

Any words of Raku wisdom?

Many thanks,
-T




Hi All,

I do believe what I am asking is not possible in
Raku.  In Modula2, it is ridiculously easy to
do. But there is a fly in the ointment.  There
is a possibility of chr(0)'s in the resultant
string.  And in Modula2, as is in C, that is
a string terminator.  So Raku is a much better
choice, as the length of a string is kept in
a hidden structure and not in the the string
itself.  And I am done with Modula2 and use
Raku for a reason.

Anyway, I have been wrapping my mind around
how to do this in Raku.  With some chr's and
ord's, I can accomplish what I need.  And
some string tricks I learned in Perl 5.

When I come up working module for this, I
will get back.  I think you all will find it
very useful.  Ya, I know I am being
a bit cryptic, but all will be revealed.
Eventually.

:-)

-T


Hi All,

Thank you for all the help and tips on this!

One of the unusual decisions Raku make concerning
string was the that would only have one element and
you could not address the indexes as you would an
array. `.elems` will always be 1.  (Use `.chars`
instead.)

But, they came up with a way to do it anyway
called `substr-rw`.

This gives me the ability to do:


BigRoot.precision = $SourceStr.chars * 2;
$BigNum= BigRoot.newton's-sqrt: $PrimeNumber;
$MyCypher  = sprintf $BigNum.base(16);
$MyCypher ~~ s/ $( Q[.] ) //;

loop  (my $Index=0; $Index < $SourceStr.chars; $Index += 1) {
   $ScrambleStr ~= chr( ord($SourceStr.substr-rw( $Index, 1 ) ) +^ 
ord( $MyCypher.substr-rw( $Index, 1 ) ) );

   # print $ScrambleStr ~ "\n";
}


And now you know what I am up to.   I am placing
a string with somewhat private information up on a
web file sharing service and needed to get around:

1) data mining
2) prying employees eyes
3) hackers

And yes, there is a hardened password.

So in other words, despite what the services say, there is zero trust.

Running the scrambled string back through the
above gets you your original text back.

Is it just me, or does `sprintf` just blow your
mind on how useful it is.  Beats the heck out of
a beginner trying to figure out the "encode"
intricacies.




My keeper on String indexes.  (I show how
the read, not just write.  The doc page
only shows how to write or I could not
find it):

Raku: reading and writing to a string's index:

Reference:
https://docs.raku.org/routine/substr-rw
https://raku.land/github:thundergnat/String::Splice

Note: Raku's Strings can not be directly addressed
  by their indexes, as you can other arrays.
  Therefore .elems will always be 1.  Use
  .chars instead

  Use `substr-rw` to work around this.


method substr-rw($from, $length = *)


> my Str $i="abcdef"
abcdef

> say $i.substr-rw(2, 1)
c

$i.substr-rw(2, 1) ="x"
x

> say $i
abxdef


> loop (my $j=0; $j < $i.chars; $j += 1) {say "$i.substr-rw($j,1)";}
a
b
x
d
e
f