Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [perl] Powers!?! Where? (https://thetfp.com/tfp/tilted-technology/47793-perl-powers-where.html)

VF19 03-03-2004 09:48 PM

[perl] Powers!?! Where?
 
How do I get a number to be multipied by its power in perl?
Im trying to solve this equation, y=c(1+r)[to the power of]t
but im stuck at where to multiply (1+r) by t.
Heres the script so far:

Quote:


#!usr/bin/perl;

use warnings;

print "You solve the y=c(1+r)t problem with this program. \n";

print "Enter c variable. \n";

my $cvar = <STDIN>;

print "Enter r variable. \n";

my $rvar = <STDIN>;

print "Enter t variable. \n";

my $tvar = <STDIN>;

chomp " $cvar , $rvar , $tvar ";

my $rvar1 = 1+r ;

my $r2tvar = $rvar1



Mephex 03-03-2004 09:50 PM

Quote:

Binary ** is the exponentiation operator. Note that it binds even more tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. Note also that ** has right associativity, so:

$e = 2 ** 3 ** 4;
evaluates to 2 to the 81st power, not 8 to the 4th power.

The * (multiply) and / (divide) operators work exactly as you might expect, multiplying or dividing their two operands. Division is done in floating-point mode, unless integer mode in enabled (via use integer).
Hope that helps !

ni42 03-03-2004 11:26 PM

somthing like this maybe?
Code:

$one = 2;
$two = 4;
$three = $one ** $two;
print $three;


ratbastid 03-11-2004 04:57 PM

Quote:

chomp " $cvar , $rvar , $tvar ";
That won't work. You're only passing one argument to chomp, and it's a string composed of each of your variables separated by a comma and a space. It's syntactically valid, but it does nothing. You're going to leave the return characters on the end of each of those $vars.

You should instead be getting your input like this:

chomp(my $cvar = <STDIN>);

That'll chomp DURING the assignment. You also save a line, which is important when you're playing Perl Golf.

And now that you know the ** operator, that formula should be a one-liner too.


All times are GMT -8. The time now is 09:11 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54