Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 03-03-2004, 09:48 PM   #1 (permalink)
Banned
 
Location: back to my old location
[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

VF19 is offline  
Old 03-03-2004, 09:50 PM   #2 (permalink)
Psycho
 
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 !
Mephex is offline  
Old 03-03-2004, 11:26 PM   #3 (permalink)
Upright
 
Location: BC, Canada
somthing like this maybe?
Code:
$one = 2;
$two = 4;
$three = $one ** $two;
print $three;
__________________
- 42 -
ni42 is offline  
Old 03-11-2004, 04:57 PM   #4 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
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.
ratbastid is offline  
 

Tags
perl, powers


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:51 AM.

Tilted Forum Project

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76