What is the power function

P

Prateek Agarwal

I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?
 
C

Chris Dempsey

[Note: parts of this message were removed to make it a legal post.]

I think you're looking for:

2 ** 3 => 8
 
P

Prateek Agarwal

Chris said:
I think you're looking for:

2 ** 3 => 8

Thanks Chris.
The following code is showing some error. Can you tell me what the
problem is?

def power(a,b)
result=a**b
result.to_i
return result
end

puts "a="
a=gets
a.to_i
puts "b="
b=gets
b.to_i
c=power(a,b)
puts "c=#{c}"
 
M

Matthew K. Williams

Thanks Chris.
The following code is showing some error. Can you tell me what the
problem is?

def power(a,b)
result=a**b
result.to_i

This does not change result. However, if you leave off the next line, it
might behave as you're desiring, provided you've passed in numbers (and
not strings, see below)
return result
end

puts "a="
a=gets

This returns a string

This evaluates as an integer, but doesn't change a
puts "b="
b=gets

This returns a string

this returns an integer but doesn't change b
c=power(a,b)

you're passing in two strings here..... You'll get an error like:

NoMethodError: undefined method `**' for "1\n":String

puts "c=#{c}"

Matt
 
G

Gregory Brown

Thanks Chris.
The following code is showing some error. Can you tell me what the
problem is?

def power(a,b)
result=a**b
result.to_i
return result
end

puts "a="
a=gets
a.to_i
puts "b="
b=gets
b.to_i
c=power(a,b)
puts "c=#{c}"

String#to_i is just a function that returns an integer, it does not
'cast' a string into an integer or anything like that.

This means you need to capture the return value to use it, or pass the
result directly to a function.

This would work:

c=power(a.to_i,b.to_i)

As would:

a = a.to_i
b = b.to_i
power(a,b)
 
B

Bertram Scharpf

Hi,

Am Dienstag, 28. Jul 2009, 02:16:44 +0900 schrieb Matthew K. Williams:
This does not change result. However, if you leave off the next line, it
might behave as you're desiring, provided you've passed in numbers (and not
strings, see below)


This returns a string


This evaluates as an integer, but doesn't change a

It _cannot_ convert objects class. "a.to_i" may return an integer
but there is no "a.to_i!" that makes a Fixnum out of a String
object. "a.class" remains "String".

I don't like the to_i method very much because it just returns
zero for wrong input. In most cases I prefer the Integer function.

Integer "123" # => 123
Integer "010" # => 8
Integer "0x10" # => 16
Integer "xxx" # raises an ArgumentError
"0x10".to_i # => 0
"xxx".to_i # => 0

Bertram
 
M

Marc Heiler

Prateek


def power(a,b)
result = (a ** b).to_i
end

puts power 2,4 # => "16"


These small snippets are best tried in irb.

You should use irb, really.
 
B

Bertram Scharpf

Hi,

Am Dienstag, 28. Jul 2009, 06:25:52 +0900 schrieb Marc Heiler:
def power(a,b)
result = (a ** b).to_i
end

The variable "result" is superfluous.

def power(a,b)
(a ** b).to_i
end

puts power 2,4

There is some more output:

(eval):5: warning: parenthesize argument(s) for future version

I never understood the necessity of this message but to be honest
the line is not very beautiful to read without parentheses.

puts power(2,4)

Bertram
 
R

RichardOnRails

I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?

Here's your program (commented out with =begin and =end statements)
and with line-by-line comments.

Following the is a candidate revision of your code, preceded by a
couple of commented lines suggesting that you save this program and
then run it.

Hope this helps.
 
R

RichardOnRails

I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?

Whoops. I forgot to paste in the program (sorry), which follows:

=begin # Note this comments out all lines until the =end
def power(a,b)
result=a**b # "a" should be "a.chomp.to_1"; ditto "b";
# the "chomp" removes the newline which the user presses
# "result" is unnecessary
result.to_i # does nothing
return result # unnecessary:
# Since we've eliminated everything else, the method
# has only one statement, i.e. the expression
# a ** b with the replacements suggested above
# Ruby returns the last statement's value
end

puts "a=" # use printf rather than puts (which appends a newline)
a=gets
a.to_i # does nothing; "a" does not get change, and the
result
# is discarded
puts "b=" # same as "a"
b=gets
b.to_i # ditto as for "a"
c=power(a,b) # numeric result assigned to c, probably an integer but
# not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
# written in Ruby as suggested below
=end

# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as: Test.rb
# and run them as: ruby Test.rb

def power(a,b)
a.chomp.to_i**b.chomp.to_i
end

printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]
 
M

Michael W. Ryder

RichardOnRails said:
I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?

Whoops. I forgot to paste in the program (sorry), which follows:

=begin # Note this comments out all lines until the =end
def power(a,b)
result=a**b # "a" should be "a.chomp.to_1"; ditto "b";
# the "chomp" removes the newline which the user presses
# "result" is unnecessary
result.to_i # does nothing
return result # unnecessary:
# Since we've eliminated everything else, the method
# has only one statement, i.e. the expression
# a ** b with the replacements suggested above
# Ruby returns the last statement's value
end

puts "a=" # use printf rather than puts (which appends a newline)
a=gets
a.to_i # does nothing; "a" does not get change, and the
result
# is discarded
puts "b=" # same as "a"
b=gets
b.to_i # ditto as for "a"
c=power(a,b) # numeric result assigned to c, probably an integer but
# not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
# written in Ruby as suggested below
=end

# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as: Test.rb
# and run them as: ruby Test.rb

def power(a,b)
a.chomp.to_i**b.chomp.to_i
end

printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]


As an "improvement" to your code I would take the chomp and to_i out of
the power function to make it more generic and add them after the gets.
 
R

RichardOnRails

Whoops.  I forgot to paste in the program (sorry),  which follows:
=begin        # Note this comments out all lines until the =end
def power(a,b)
result=a**b   # "a" should be "a.chomp.to_1"; ditto "b";
              # the "chomp" removes the newline which theuser presses
              # "result" is unnecessary
result.to_i   # does nothing
return result # unnecessary:
              # Since we've eliminated everything else, the method
              # has only one statement, i.e. the expression
              # a ** b with the replacements suggested above
              # Ruby returns the last statement's value
end
puts "a="     # use printf rather than puts (which appends a newline)
a=gets
a.to_i        # does nothing; "a" does not get change, and the
result
              # is discarded
puts "b="     # same as "a"
b=gets
b.to_i           # ditto as for "a"
c=power(a,b)  # numeric result assigned to c, probably an integer but
              # not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
              # written in Ruby as suggested below
=end
# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as:       Test.rb
# and run them as:                              ruby Test.rb
def power(a,b)
  a.chomp.to_i**b.chomp.to_i
end
printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]

As an "improvement" to your code I would take the chomp and to_i out of
the power function to make it more generic and add them after the gets.

Hi Michael,

Your point is well taken.

I did that for a newbie to point the stuff that's needs to be done to
get things working as he intends. He's not likely to look up "to_i"
to learn all its machinations.

In fact, there's one more that I would have thrown in, had I
remembered it: strip.

I do that in string-handlers I write:
1. in part, to remind myself what to_i would do for me automatically
2. in part, to guarantee that that stuff gets done even if new
versions of Ruby eliminate some helpful feature.
3. in part, because I might decide to extend a program using the input
string as though it contained only the digits that to_i revealed,
forgetting that a lot of "baggage" had been removed.

Perhaps having taught Computer Technology at AU in DC for a decade
gives me a different perspective than production program with a lean-
and-mean code perspective.

Do I make any sense?

Best wishes,
Richard
 
R

RichardOnRails

RichardOnRails said:
I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?
--
Posted viahttp://www.ruby-forum.com/.
Whoops.  I forgot to paste in the program (sorry),  which follows:
=begin        # Note this comments out all lines until the =end
def power(a,b)
result=a**b   # "a" should be "a.chomp.to_1"; ditto "b";
              # the "chomp" removes the newline which the user presses
              # "result" is unnecessary
result.to_i   # does nothing
return result # unnecessary:
              # Since we've eliminated everything else,the method
              # has only one statement, i.e. the expression
              # a ** b with the replacements suggested above
              # Ruby returns the last statement's value
end
puts "a="     # use printf rather than puts (which appends a newline)
a=gets
a.to_i        # does nothing; "a" does not get change, and the
result
              # is discarded
puts "b="     # same as "a"
b=gets
b.to_i           # ditto as for "a"
c=power(a,b)  # numeric result assigned to c, probably an integerbut
              # not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
              # written in Ruby as suggested below
=end
# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as:       Test.rb
# and run them as:                              ruby Test.rb
def power(a,b)
  a.chomp.to_i**b.chomp.to_i
end
printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]
As an "improvement" to your code I would take the chomp and to_i out of
the power function to make it more generic and add them after the gets.

Hi Michael,

Your point is well taken.

I did that for a newbie to point the stuff that's needs to be done to
get things working as he intends.  He's not likely to look up "to_i"
to learn all its machinations.

In fact, there's one more that I would have thrown in, had I
remembered it: strip.

I do that in string-handlers I write:
1. in part, to remind myself what to_i would do for me automatically
2. in part, to guarantee that that stuff gets done even if new
versions of Ruby eliminate some helpful feature.
3. in part, because I might decide to extend a program using the input
string as though it contained only the digits that to_i revealed,
forgetting that a lot of "baggage" had been removed.

Perhaps having taught Computer Technology at AU in DC for a decade
gives me a different perspective than production program with a lean-
and-mean code perspective.

Do I make any sense?

Best wishes,
Richard

Hey Michael,

After posting my reply to you, I re-read you post and realized I
misinterpreted it. I favor putting all that baggage in the OP's
"power" routine rather than having to remember that stuff when writing
each invocation of "power".

Does that make any sense?

Best wishes,
Richard
 
M

Michael W. Ryder

RichardOnRails said:
RichardOnRails wrote:
I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?
--
Posted viahttp://www.ruby-forum.com/.
Whoops. I forgot to paste in the program (sorry), which follows:
=begin # Note this comments out all lines until the =end
def power(a,b)
result=a**b # "a" should be "a.chomp.to_1"; ditto "b";
# the "chomp" removes the newline which the user presses
# "result" is unnecessary
result.to_i # does nothing
return result # unnecessary:
# Since we've eliminated everything else, the method
# has only one statement, i.e. the expression
# a ** b with the replacements suggested above
# Ruby returns the last statement's value
end
puts "a=" # use printf rather than puts (which appends a newline)
a=gets
a.to_i # does nothing; "a" does not get change, and the
result
# is discarded
puts "b=" # same as "a"
b=gets
b.to_i # ditto as for "a"
c=power(a,b) # numeric result assigned to c, probably an integer but
# not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
# written in Ruby as suggested below
=end
# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as: Test.rb
# and run them as: ruby Test.rb
def power(a,b)
a.chomp.to_i**b.chomp.to_i
end
printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]
As an "improvement" to your code I would take the chomp and to_i out of
the power function to make it more generic and add them after the gets.
Hi Michael,

Your point is well taken.

I did that for a newbie to point the stuff that's needs to be done to
get things working as he intends. He's not likely to look up "to_i"
to learn all its machinations.

In fact, there's one more that I would have thrown in, had I
remembered it: strip.

I do that in string-handlers I write:
1. in part, to remind myself what to_i would do for me automatically
2. in part, to guarantee that that stuff gets done even if new
versions of Ruby eliminate some helpful feature.
3. in part, because I might decide to extend a program using the input
string as though it contained only the digits that to_i revealed,
forgetting that a lot of "baggage" had been removed.

Perhaps having taught Computer Technology at AU in DC for a decade
gives me a different perspective than production program with a lean-
and-mean code perspective.

Do I make any sense?

Best wishes,
Richard

Hey Michael,

After posting my reply to you, I re-read you post and realized I
misinterpreted it. I favor putting all that baggage in the OP's
"power" routine rather than having to remember that stuff when writing
each invocation of "power".

Does that make any sense?

Best wishes,
Richard

The reason I stripped out the conversions from the power function is to
allow its use with either integers or floats. Plus I prefer to convert
input to it's desired form as soon as possible, rather than having to
remember to convert it everywhere it is used. If a is an integer there
is no need to store it as a string with \n on the end and then having to
remember to chomp and to_i it everywhere you need it. What if the OP
wanted to use a and b later such as showing the power of a and b and
then the power of a+1 and b+1? He would have to again chomp the
variables and convert them to integers, add one to each, then convert
them back to strings before feeding them to the power function.
 
R

RichardOnRails

RichardOnRails said:
On Jul 28, 3:34 pm, "Michael W. Ryder" <[email protected]>
wrote:
RichardOnRails wrote:
I am new to Ruby and am still learning some of the basic stuff.
What's the method name for the Power operation(as in 'a' to the power
'b')?
--
Posted viahttp://www.ruby-forum.com/.
Whoops.  I forgot to paste in the program (sorry),  which follows:
=begin        # Note this comments out all lines until the=end
def power(a,b)
result=a**b   # "a" should be "a.chomp.to_1"; ditto "b";
              # the "chomp" removes the newline which the user presses
              # "result" is unnecessary
result.to_i   # does nothing
return result # unnecessary:
              # Since we've eliminated everything else, the method
              # has only one statement, i.e. the expression
              # a ** b with the replacements suggestedabove
              # Ruby returns the last statement's value
end
puts "a="     # use printf rather than puts (which appends a newline)
a=gets
a.to_i        # does nothing; "a" does not get change, and the
result
              # is discarded
puts "b="     # same as "a"
b=gets
b.to_i           # ditto as for "a"
c=power(a,b)  # numeric result assigned to c, probably an integer but
              # not necessarily
puts "c=#{c}" # These final two lines might be more elegantly
              # written in Ruby as suggested below
=end
# The result of all these changes are the following 8 lines
# (plus blank lines); save them, say, as:       Test.rb
# and run them as:                              ruby Test.rb
def power(a,b)
  a.chomp.to_i**b.chomp.to_i
end
printf "a="
a = gets
print "b="
b = gets
puts "%d**%d = %d" % [a, b, power(a,b)]
As an "improvement" to your code I would take the chomp and to_i out of
the power function to make it more generic and add them after the gets.
Hi Michael,
Your point is well taken.
I did that for a newbie to point the stuff that's needs to be done to
get things working as he intends.  He's not likely to look up "to_i"
to learn all its machinations.
In fact, there's one more that I would have thrown in, had I
remembered it: strip.
I do that in string-handlers I write:
1. in part, to remind myself what to_i would do for me automatically
2. in part, to guarantee that that stuff gets done even if new
versions of Ruby eliminate some helpful feature.
3. in part, because I might decide to extend a program using the input
string as though it contained only the digits that to_i revealed,
forgetting that a lot of "baggage" had been removed.
Perhaps having taught Computer Technology at AU in DC for a decade
gives me a different perspective than production program with a lean-
and-mean code perspective.
Do I make any sense?
Best wishes,
Richard
Hey Michael,
After posting my reply to you,  I re-read you post and realized I
misinterpreted it.  I favor putting all that baggage in the OP's
"power" routine rather than having to remember that stuff when writing
each invocation of "power".
Does that make any sense?
Best wishes,
Richard

The reason I stripped out the conversions from the power function is to
allow its use with either integers or floats.  Plus I prefer to convert
input to it's desired form as soon as possible, rather than having to
remember to convert it everywhere it is used.  If a is an integer there
is no need to store it as a string with \n on the end and then having to
remember to chomp and to_i it everywhere you need it.  What if the OP
wanted to use a and b later such as showing the power of a and b and
then the power of a+1 and b+1?  He would have to again chomp the
variables and convert them to integers, add one to each, then convert
them back to strings before feeding them to the power function.

Good points, Michael.

Let's hope the OP learned something from all of this.

Best wishes,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top