Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
polyfractal opened this issue Sep 28, 2013 · 5 comments
Closed

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

polyfractal opened this issue Sep 28, 2013 · 5 comments

Comments

@polyfractal
Copy link

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);
@bluss
Copy link
Member

bluss commented Sep 28, 2013

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
Copy link
Contributor

lilac commented Jan 22, 2014

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
Copy link
Member

huonw commented Jan 22, 2014

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).

@aturon aturon added the A-libs label Jun 3, 2014
@frewsxcv
Copy link
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
Copy link
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.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 21, 2022
[arithmetic-side-effects] Detect overflowing associated constants of integers

Triggers the negation of maximum unsigned integers using associated constants. Rustc already handles `-128i8` but doesn't handle `-i8::MAX`.

At the same time, allows stuff like `-1234`.

changelog: FP: [arithmetic-side-effects] Detect overflowing associated constants of integers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants