Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to store all 1 s in perl

by suvendra123 (Initiate)
on Apr 08, 2021 at 01:15 UTC ( [id://11130981]=perlquestion: print w/replies, xml ) Need Help??

suvendra123 has asked for the wisdom of the Perl Monks concerning the following question:

How to store all 1 s in perl

my $value = 1 x 131/ 1; my $value1 = $value; print "$value";

output is coming as

1.11111111111111e+130

But I want variable $value1 as all 1's 131 times instead of e+130

What is the right step.

Replies are listed 'Best First'.
Re: How to store all 1 s in perl
by Fletch (Bishop) on Apr 08, 2021 at 01:30 UTC

    Not that this isn't going to fall on deaf ears . . .

    You're treating it like a number so you're getting a number. Don't do that.

    $ perl -MDevel::Peek=Dump -E 'Dump( 1 x 131 / 1 );Dump( 1 x 131 )' SV = NV(0x7f9a970202f0) at 0x7f9a97020308 REFCNT = 1 FLAGS = (PADTMP,NOK,READONLY,PROTECT,pNOK) NV = 1.11111111111111e+130 SV = PVIV(0x7f9a97020c38) at 0x7f9a970202c0 REFCNT = 1 FLAGS = (PADTMP,POK,READONLY,PROTECT,pPOK) IV = 1 PV = 0x7f9a96c1d730 "11111111111111111111111111111111111111111111111 +111111111111111111111111111111111111111111111111111111111111111111111 +111111111111111"\0 CUR = 131 LEN = 136

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to store all 1 s in perl
by tybalt89 (Monsignor) on Apr 08, 2021 at 02:07 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11130981 use warnings; use bigrat; my $value = 1 x 131/ 1; my $value1 = $value; print "$value\n";

    Outputs:

    1111111111111111111111111111111111111111111111111111111111111111111111 +1111111111111111111111111111111111111111111111111111111111111

    hehehehehehe !!

Re: How to store all 1 s in perl
by hrcerq (Scribe) on Apr 08, 2021 at 17:27 UTC

    I'm not sure if I get what you want. If what you want is to print number one 131 times, then you should just remove the "/ 1" part of the first line, as it's treating your desired result (number one 131 times) as a number instead of a string, and as it's a big number, it's displayed in scientific notation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11130981]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found