Skip to content

Pow() syntax is inconsistent between floats and ints #9592

@polyfractal

Description

@polyfractal

Brought this up on IRC the other day and was told to make an issue :)

The current syntax for Pow() is confusing/inconsistent for (ignorant) newcomers like myself. For floats, you can do:

let my_float = 5f32;
let pow2 = my_float.pow(2);

But for any of the integers, you need to use the "static" trait function since integers do not implement the Algebraic trait:

let my_int = 5u32;
let pow2 = Int::pow(my_int, 2);

Activity

bluss

bluss commented on Sep 28, 2013

@bluss
Member

Just a correction, the syntax for the float case has to be one of:

 5.0f.pow(&2.0)
 std::num::pow(5.0, 2.0)
lilac

lilac commented on Jan 22, 2014

@lilac
Contributor

As in the current master branch, there are three pow variants:

std::f32::pow   
std::f64::pow   
std::num::pow

All of them are plain functions, so 5.0f.pow(&2.0) doesn't work now.

Can this issue be closed then?

huonw

huonw commented on Jan 22, 2014

@huonw
Member

There's actually more than three version of "power", and 5.0.pow(&2.0) was replaced with .powf.

// trait methods
Real.powf

// freestanding
f32::pow
f64::pow
num::pow // (generic base, integer exponent)
num::powf // (generic Real base & exponent)

In any case, I think the f32 and f64 freestanding pow's should be removed or made private (if possible).

frewsxcv

frewsxcv commented on Jan 24, 2015

@frewsxcv
Member

A quick update: a pow() method was added to the Int trait in this pull request, despite it not existing in the numerics reform RFC

steveklabnik

steveklabnik commented on Feb 15, 2015

@steveklabnik
Member

This is now all consistent, as far as I can tell. Int has pow, and Float's was changed to powi and powf, but the method syntax works.

added a commit that references this issue on Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@frewsxcv@lilac@aturon@huonw

        Issue actions

          Pow() syntax is inconsistent between floats and ints · Issue #9592 · rust-lang/rust