Ruby Exponents

Hi,

I’m trying to calculate using exponential operator **. However, I get a
crash with the following message

wrong argument type Float (expected Fixnum)

The values I am using are 0.713 ** 0.5555555 => THIS CRASHES

But when I try this on a ruby command prompt, it works fine. Is there a
way I can calculate using a float number as an exponent?

Thanks.

Tony

On Wednesday 02 July 2008, Tony Dermont wrote:

way I can calculate using a float number as an exponent?

Thanks.

Tony

Are you sure the error is due to that computation? It works perfectly
for me,
either in irb, passing it to ruby -e or putting it in a script.

Stefano

Yeah even I dont any errors either through the ruby script or IRB …can
you
paste the error you get when you use the script
kranthi.

On Wed, Jul 2, 2008 at 2:08 PM, Stefano C. [email protected]

Thanks for all the replies. I just can’t figure it out. I placed the
same set code in just a ruby file and it runs fine, but when I have it
inside a rails model it crashed with this crash message on my browser.
If I put in an integer value for the exponent, it does not crash.

app/models/trade.rb:137:in **' app/models/trade.rb:137:incalculate_cagr’
app/views/adminportfolio/ajax_disport.rhtml:96:in
_run_erb_47app47views47adminportfolio47ajax_disport46rhtml' app/views/adminportfolio/ajax_disport.rhtml:69:ineach’
app/views/adminportfolio/ajax_disport.rhtml:69:in
_run_erb_47app47views47adminportfolio47ajax_disport46rhtml' app/views/adminportfolio/ajax_disport.rhtml:67:in_run_erb_47app47views47adminportfolio47ajax_disport46rhtml’

This might be a rails issue, but if anyone can help me out I’d truly
truly appreciate it.

Tony

Tony Dermont wrote:

way I can calculate using a float number as an exponent?

Thanks.

Tony

znmeb@DreamScape ~ $ echo ‘puts 0.713 ** 0.5555555’ > script.rb
znmeb@DreamScape ~ $ ruby script.rb
0.828672792977234
znmeb@DreamScape ~ $ ruby -e ’ puts 0.713 ** 0.5555555’
0.828672792977234
znmeb@DreamScape ~ $ irb
irb(main):001:0> 0.713 ** 0.5555555
=> 0.828672792977234
irb(main):002:0> quit
znmeb@DreamScape ~ $ ruby --version
ruby 1.8.6 (2008-06-20 patchlevel 230) [x86_64-linux]
znmeb@DreamScape ~ $

So there’s nothing wrong with your code. What version of Ruby do you
have? Maybe it needs to be upgraded or re-installed.

I am hitting the exact same issue! :frowning:

Tony Dermont wrote in post #697257:

Thanks for all the replies. I just can’t figure it out. I placed the
same set code in just a ruby file and it runs fine, but when I have it
inside a rails model it crashed with this crash message on my browser.
If I put in an integer value for the exponent, it does not crash.

app/models/trade.rb:137:in **' app/models/trade.rb:137:incalculate_cagr’
app/views/adminportfolio/ajax_disport.rhtml:96:in
_run_erb_47app47views47adminportfolio47ajax_disport46rhtml' app/views/adminportfolio/ajax_disport.rhtml:69:ineach’
app/views/adminportfolio/ajax_disport.rhtml:69:in
_run_erb_47app47views47adminportfolio47ajax_disport46rhtml' app/views/adminportfolio/ajax_disport.rhtml:67:in_run_erb_47app47views47adminportfolio47ajax_disport46rhtml’

This might be a rails issue, but if anyone can help me out I’d truly
truly appreciate it.

  • Show line 137 of app/models/trade.rb, plus a few lines either side.

  • Show the contents of the expressions you are combining. e.g.

    $stderr.puts foo.inspect
    $stderr.puts bar.inspect
    baz = bar ** foo

aswath k. wrote in post #1013942:

I am hitting the exact same issue! :frowning:

Not me. :slight_smile:

Also do this:

$stderr.puts foo.class

Is it possible that one of your arguments is a BigDecimal?

$ irb --simple-prompt

require ‘bigdecimal’
=> true

foo = BigDecimal.new(“0.713”)
=> #BigDecimal:7fc0698c7180,‘0.713E0’,9(18)

foo ** 0.5555555
TypeError: wrong argument type Float (expected Fixnum)
from (irb):3:in `**’
from (irb):3
from :0

(this will be fixed in 1.9.3, according to NEWS file)