School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,714 people online right now. Registration is fast and FREE... Join Now!




Perl program to calculate an equation

 

Perl program to calculate an equation, Take user's input and calculate its output

romeyb1105

29 Oct, 2009 - 07:32 PM
Post #1

New D.I.C Head
*

Joined: 9 Feb, 2009
Posts: 9


My Contributions
Here's my code for a problem that I'm working on in a Perl Programming book:

CODE
print "Please enter a number:  ";
$a = <STDIN>;
print "Please enter a number:  ";
$b = <STDIN>;
print "Please enter a number:  ";
$c = <STDIN>;

$v = (8*$a*$b) - ($c/4) >> 17.32;

print "The value is $v";


I can enter the numbers just fine, but the output always comes to zero. What's the next step I need to take in order to get the program to calculate the above equation?




User is offlineProfile CardPM
+Quote Post


dsherohman

RE: Perl Program To Calculate An Equation

30 Oct, 2009 - 05:03 AM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(romeyb1105 @ 30 Oct, 2009 - 03:32 AM) *

CODE

$v = (8*$a*$b) - ($c/4) >> 17.32;


I can enter the numbers just fine, but the output always comes to zero. What's the next step I need to take in order to get the program to calculate the above equation?

It's calculating it correctly, exactly as you specified it... but ">>" is probably not doing what you think it is.

From 'man perlop':
QUOTE
Binary ">>" returns the value of its left argument shifted right by the
number of bits specified by the right argument. Arguments should be
integers. (See also "Integer Arithmetic".)


You've told Perl to calculate the value of (8*$a*$b ) - ($c/4)... and then shift the result right by 17.32 bits. Assuming Perl rounds the 17.32 down to 17, this is roughly equivalent to dividing by 131,072 (which is 2^17) and rounding down. If (8*$a*$b ) - ($c/4) is larger than 131,072, then you should get a non-zero result.

So what did you intend the ">>" to do?
User is offlineProfile CardPM
+Quote Post

romeyb1105

RE: Perl Program To Calculate An Equation

30 Oct, 2009 - 11:07 AM
Post #3

New D.I.C Head
*

Joined: 9 Feb, 2009
Posts: 9


My Contributions
QUOTE(dsherohman @ 30 Oct, 2009 - 05:03 AM) *

QUOTE(romeyb1105 @ 30 Oct, 2009 - 03:32 AM) *

CODE

$v = (8*$a*$b) - ($c/4) >> 17.32;


I can enter the numbers just fine, but the output always comes to zero. What's the next step I need to take in order to get the program to calculate the above equation?

It's calculating it correctly, exactly as you specified it... but ">>" is probably not doing what you think it is.

From 'man perlop':
QUOTE
Binary ">>" returns the value of its left argument shifted right by the
number of bits specified by the right argument. Arguments should be
integers. (See also "Integer Arithmetic".)


You've told Perl to calculate the value of (8*$a*$b ) - ($c/4)... and then shift the result right by 17.32 bits. Assuming Perl rounds the 17.32 down to 17, this is roughly equivalent to dividing by 131,072 (which is 2^17) and rounding down. If (8*$a*$b ) - ($c/4) is larger than 131,072, then you should get a non-zero result.

So what did you intend the ">>" to do?



QUOTE(dsherohman @ 30 Oct, 2009 - 05:03 AM) *

QUOTE(romeyb1105 @ 30 Oct, 2009 - 03:32 AM) *

CODE

$v = (8*$a*$b) - ($c/4) >> 17.32;


I can enter the numbers just fine, but the output always comes to zero. What's the next step I need to take in order to get the program to calculate the above equation?

It's calculating it correctly, exactly as you specified it... but ">>" is probably not doing what you think it is.

From 'man perlop':
QUOTE
Binary ">>" returns the value of its left argument shifted right by the
number of bits specified by the right argument. Arguments should be
integers. (See also "Integer Arithmetic".)


You've told Perl to calculate the value of (8*$a*$b ) - ($c/4)... and then shift the result right by 17.32 bits. Assuming Perl rounds the 17.32 down to 17, this is roughly equivalent to dividing by 131,072 (which is 2^17) and rounding down. If (8*$a*$b ) - ($c/4) is larger than 131,072, then you should get a non-zero result.

So what did you intend the ">>" to do?



I must've misunderstood how the shift operators work. I'm trying to do the Please Excuse My Dear Aunt Sally method of the equation so I can get the correct answer.

User is offlineProfile CardPM
+Quote Post

romeyb1105

RE: Perl Program To Calculate An Equation

30 Oct, 2009 - 11:32 AM
Post #4

New D.I.C Head
*

Joined: 9 Feb, 2009
Posts: 9


My Contributions
I fixed it. It's working now. Thank you for your help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:13PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month