Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:59:31+00:00 2026-05-27T20:59:31+00:00

In ruby-doc , it says that <Fixnum> ** <Numeric> may be fractional, and gives

  • 0

In ruby-doc, it says that <Fixnum> ** <Numeric> may be fractional, and gives the examples:

2 ** -1 #=> 0.5
2 ** 0.5 #=> 1.4142135623731

but on my irb, it sometimes gives a Rational answer as with the exponent -1 below:

2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731

It looks like ruby-doc is not accurate, and ruby tries to type cast to Rational when possible, but I am not completely sure. What is the exact type casting rule here when the base and the exponent are both Fixnum? I am particularly interested in Ruby 1.9.3, but is the result different among different versions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:59:31+00:00Added an answer on May 27, 2026 at 8:59 pm

    DGM is right; the answer is right in the docs you linked, although it’s in C. Here is pertinent bit; I’ve added a few comments:

    static VALUE
    fix_pow(VALUE x, VALUE y)
    {
        long a = FIX2LONG(x);
    
        if (FIXNUM_P(y)) {          // checks to see if Y is a Fixnum
            long b = FIX2LONG(y);
    
            if (b < 0)
                // if b is less than zero, convert x into a Rational
                // and call ** on it and 1 over y
                // (this is how you raise to a negative power).
                return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
    

    Now we can move on to the docs for Rational and check what it says about the ** operator:

    rat ** numeric → numeric
    

    Performs exponentiation.

    For example:

    Rational(2)    ** Rational(3)    #=> (8/1)
    Rational(10)   ** -2             #=> (1/100)
    Rational(10)   ** -2.0           #=> 0.01
    Rational(-4)   ** Rational(1,2)  #=> (1.2246063538223773e-16+2.0i)
    Rational(1, 2) ** 0              #=> (1/1)
    Rational(1, 2) ** 0.0            #=> 1.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 3rc app on Ruby 1.9.2 that works fine, but Bundler
Ruby-doc has this description: The ObjectSpace module contains a number of routines that interact
According to the ruby-doc: Hashes enumerate their values in the order that the corresponding
rvm install 1.9.2 gives: [2011-08-31 00:54:45] ./configure --prefix=/home/pklingem/.rvm/rubies/ruby-1.9.2-p290 --enable-shared --disable-install-doc --with-libyaml-dir=/home/pklingem/.rvm/usr configure: WARNING: unrecognized
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Ruby on Rails has magic timestamping fields that are automatically updated when a record
Ruby is truly memory-hungry - but also worth every single bit. What do you
Ruby Net::IMAP library provides a good API for IMAP stuff ( http://railsapi.com/doc/ruby-v1.8/classes/Net/IMAP.html ). There
This Ruby code using Nokogiri doc.xpath(//tbody).remove removes the children of the <tbody> (as well
My Rails app passes its test but then says 0% passed. Its a Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.