Skip to content

Add function aliases for float operators, and clean up other numeric functions #1082

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/comp/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import std::fs;
import std::os_fs;
import std::vec;
import std::map;
import std::math;
import std::str;
import std::uint;
import metadata::cstore;
Expand Down Expand Up @@ -128,7 +129,7 @@ fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
assert len1 > 0u;
assert len2 > 0u;

let max_common_path = uint::min(len1, len2) - 1u;
let max_common_path = math::min(len1, len2) - 1u;
let start_idx = 0u;
while start_idx < max_common_path
&& split1[start_idx] == split2[start_idx] {
Expand Down
3 changes: 2 additions & 1 deletion src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import std::box;
import std::ufind;
import std::map;
import std::map::hashmap;
import std::math;
import std::option;
import std::option::none;
import std::option::some;
Expand Down Expand Up @@ -1812,7 +1813,7 @@ mod unify {
// Unifies two sets.
fn union(cx: @ctxt, set_a: uint, set_b: uint,
variance: variance) -> union_result {
ufind::grow(cx.vb.sets, uint::max(set_a, set_b) + 1u);
ufind::grow(cx.vb.sets, math::max(set_a, set_b) + 1u);
let root_a = ufind::find(cx.vb.sets, set_a);
let root_b = ufind::find(cx.vb.sets, set_b);

Expand Down
6 changes: 3 additions & 3 deletions src/fuzzer/fuzzer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std;
use rustc;

import std::{fs, io, getopts, vec, str, int, uint, option};
import std::{fs, io, getopts, math, vec, str, int, uint, option};
import std::getopts::{optopt, opt_present, opt_str};
import std::io::stdout;

Expand Down Expand Up @@ -242,9 +242,9 @@ fn check_variants_T<T>(
let L = vec::len(things);

if L < 100u {
under(uint::min(L, 20u)) {|i|
under(math::min(L, 20u)) {|i|
log_err "Replacing... #" + uint::str(i);
under(uint::min(L, 30u)) {|j|
under(math::min(L, 30u)) {|j|
log_err "With... " + stringifier(@things[j]);
let crate2 = @replacer(crate, i, things[j], cx.mode);
// It would be best to test the *crate* for stability, but testing the
Expand Down
45 changes: 45 additions & 0 deletions src/lib/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,51 @@ fn neg_infinity() -> float {
ret -1./0.;
}

/* Function: add */
pure fn add(x: float, y: float) -> float { ret x + y; }

/* Function: sub */
pure fn sub(x: float, y: float) -> float { ret x - y; }

/* Function: mul */
pure fn mul(x: float, y: float) -> float { ret x * y; }

/* Function: div */
pure fn div(x: float, y: float) -> float { ret x / y; }

/* Function: rem */
pure fn rem(x: float, y: float) -> float { ret x % y; }

/* Predicate: lt */
pure fn lt(x: float, y: float) -> bool { ret x < y; }

/* Predicate: le */
pure fn le(x: float, y: float) -> bool { ret x <= y; }

/* Predicate: eq */
pure fn eq(x: float, y: float) -> bool { ret x == y; }

/* Predicate: ne */
pure fn ne(x: float, y: float) -> bool { ret x != y; }

/* Predicate: ge */
pure fn ge(x: float, y: float) -> bool { ret x >= y; }

/* Predicate: gt */
pure fn gt(x: float, y: float) -> bool { ret x > y; }

/* Predicate: positive */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For floats, 0.0 should be positive and -0.0 negative, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would seem to be the case. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbrubeck, do you want to try to implement that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbrubeck/rust@5fa7e614c88 changes float::positive and float::negative to be true for 0.0 and -0.0, respectively. I have no strong opinion about whether this is an improvement or not.

Note that this makes float::positive return the same thing as float::nonnegative, and float::negative return the same as float::nonpositive. Is it worth keeping the redundant functions around for consistency with std::int, or is it just confusing?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to have nonnegative and nonpositive also be false for nan instead of being strictly !positive or !negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to fix NaN behavior in mbrubeck/rust@af645e82c1, but I ran into Issue #1083.

pure fn positive(x: float) -> bool { ret x > 0.; }

/* Predicate: negative */
pure fn negative(x: float) -> bool { ret x < 0.; }

/* Predicate: nonpositive */
pure fn nonpositive(x: float) -> bool { ret x <= 0.; }

/* Predicate: nonnegative */
pure fn nonnegative(x: float) -> bool { ret x >= 0.; }

//
// Local Variables:
// mode: rust
Expand Down
26 changes: 26 additions & 0 deletions src/lib/u8.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
/*
Module: u8
*/

/*
Function: max_value

The maximum value of a u8.
*/
pure fn max_value() -> u8 { ret 255u8; }

/*
Function: min_value

The minumum value of a u8.
*/
pure fn min_value() -> u8 { ret 0u8; }

/* Function: add */
pure fn add(x: u8, y: u8) -> u8 { ret x + y; }

/* Function: sub */
pure fn sub(x: u8, y: u8) -> u8 { ret x - y; }

/* Function: mul */
pure fn mul(x: u8, y: u8) -> u8 { ret x * y; }

/* Function: div */
pure fn div(x: u8, y: u8) -> u8 { ret x / y; }

/* Function: rem */
pure fn rem(x: u8, y: u8) -> u8 { ret x % y; }

/* Predicate: lt */
pure fn lt(x: u8, y: u8) -> bool { ret x < y; }

/* Predicate: le */
pure fn le(x: u8, y: u8) -> bool { ret x <= y; }

/* Predicate: eq */
pure fn eq(x: u8, y: u8) -> bool { ret x == y; }

/* Predicate: ne */
pure fn ne(x: u8, y: u8) -> bool { ret x != y; }

/* Predicate: ge */
pure fn ge(x: u8, y: u8) -> bool { ret x >= y; }

/* Predicate: gt */
pure fn gt(x: u8, y: u8) -> bool { ret x > y; }

fn range(lo: u8, hi: u8, it: block(u8)) {
Expand Down
95 changes: 77 additions & 18 deletions src/lib/uint.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
/**
* Return the minimal value for an uint.
*
* This is always 0
*/
/*
Module: uint
*/

/*
Function: min_value

Return the minimal value for an uint.

This is always 0
*/
pure fn min_value() -> uint { ret 0u; }

/**
* Return the maximal value for an uint.
*
* This is 2^wordsize - 1
*/
/*
Function: max_value

Return the maximal value for an uint.

This is 2^wordsize - 1
*/
pure fn max_value() -> uint {
ret 0u - 1u;
}

fn add(x: uint, y: uint) -> uint { ret x + y; }
/* Function: add */
pure fn add(x: uint, y: uint) -> uint { ret x + y; }

fn sub(x: uint, y: uint) -> uint { ret x - y; }
/* Function: sub */
pure fn sub(x: uint, y: uint) -> uint { ret x - y; }

fn mul(x: uint, y: uint) -> uint { ret x * y; }
/* Function: mul */
pure fn mul(x: uint, y: uint) -> uint { ret x * y; }

fn div(x: uint, y: uint) -> uint { ret x / y; }
/* Function: div */
pure fn div(x: uint, y: uint) -> uint { ret x / y; }

fn rem(x: uint, y: uint) -> uint { ret x % y; }
/* Function: rem */
pure fn rem(x: uint, y: uint) -> uint { ret x % y; }

/* Predicate: lt */
pure fn lt(x: uint, y: uint) -> bool { ret x < y; }

/* Predicate: le */
pure fn le(x: uint, y: uint) -> bool { ret x <= y; }

/* Predicate: eq */
pure fn eq(x: uint, y: uint) -> bool { ret x == y; }

/* Predicate: ne */
pure fn ne(x: uint, y: uint) -> bool { ret x != y; }

/* Predicate: ge */
pure fn ge(x: uint, y: uint) -> bool { ret x >= y; }

/* Predicate: gt */
pure fn gt(x: uint, y: uint) -> bool { ret x > y; }

fn max(x: uint, y: uint) -> uint { if x > y { ret x; } ret y; }

fn min(x: uint, y: uint) -> uint { if x > y { ret y; } ret x; }
/*
Function: range

Iterate over the range [`lo`..`hi`)
*/
fn range(lo: uint, hi: uint, it: block(uint)) {
while lo < hi { it(lo); lo += 1u; }
}

/*
Function: next_power_of_two

Returns the smallest power of 2 greater than or equal to `n`
*/
fn next_power_of_two(n: uint) -> uint {
let halfbits: uint = sys::size_of::<uint>() * 4u;
let tmp: uint = n - 1u;
Expand All @@ -52,6 +77,20 @@ fn next_power_of_two(n: uint) -> uint {
ret tmp + 1u;
}

/*
Function: parse_buf

Parse a buffer of bytes

Parameters:

buf - A byte buffer
radix - The base of the number

Failure:

buf must not be empty
*/
fn parse_buf(buf: [u8], radix: uint) -> uint {
if vec::len::<u8>(buf) == 0u {
log_err "parse_buf(): buf is empty";
Expand All @@ -69,8 +108,22 @@ fn parse_buf(buf: [u8], radix: uint) -> uint {
fail;
}

/*
Function: from_str

Parse a string to an int

Failure:

s must not be empty
*/
fn from_str(s: str) -> uint { parse_buf(str::bytes(s), 10u) }

/*
Function: to_str

Convert to a string in a given base
*/
fn to_str(num: uint, radix: uint) -> str {
let n = num;
assert (0u < radix && radix <= 16u);
Expand Down Expand Up @@ -106,6 +159,12 @@ fn to_str(num: uint, radix: uint) -> str {
while len != 0u { len -= 1u; s1 += str::unsafe_from_byte(s[len]); }
ret s1;
}

/*
Function: str

Convert to a string
*/
fn str(i: uint) -> str { ret to_str(i, 10u); }

// Local Variables:
Expand Down