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

Fix #1015 #1030

Merged
merged 4 commits into from
Oct 12, 2011
Merged
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
2 changes: 1 addition & 1 deletion src/comp/driver/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn time<@T>(do_it: bool, what: str, thunk: fn() -> T) -> T {
let rv = thunk();
let end = std::time::precise_time_s();
log_err #fmt["time: %s took %s s", what,
std::float::float_to_str(end - start, 3u)];
std::float::to_str(end - start, 3u)];
ret rv;
}

Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ fn valid_range_bounds(l1: @ast::lit, l2: @ast::lit) -> bool {
alt l1.node {
ast::lit_float(s1) | ast::lit_mach_float(_, s1) {
let s2 = lit_as_float(l2);
let f1 = std::float::str_to_float(s1);
let f2 = std::float::str_to_float(s2);
let f1 = std::float::from_str(s1);
let f2 = std::float::from_str(s2);
ret *util::common::min(f1, f2) == f1
}
ast::lit_uint(_) | ast::lit_char(_) {
Expand Down
6 changes: 3 additions & 3 deletions src/comp/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ fn lit_in_range(l: @ast::lit, m1: @ast::lit, m2: @ast::lit) -> bool {
frange(f1, f2) {
alt l.node {
ast::lit_float(f3) | ast::lit_mach_float(_, f3) {
std::float::str_to_float(f3) >= *min(f1, f2) &&
std::float::str_to_float(f3) <= *max(f1, f2)
std::float::from_str(f3) >= *min(f1, f2) &&
std::float::from_str(f3) <= *max(f1, f2)
}
_ { fail }
}
Expand Down Expand Up @@ -232,7 +232,7 @@ fn lits_to_range(l: @ast::lit, r: @ast::lit) -> range {
}
ast::lit_float(f1) | ast::lit_mach_float(_, f1) {
alt r.node { ast::lit_float(f2) | ast::lit_mach_float(_, f2) {
frange(std::float::str_to_float(f1), std::float::str_to_float(f2))
frange(std::float::from_str(f1), std::float::from_str(f2))
}
_ { fail } }
}
Expand Down
191 changes: 177 additions & 14 deletions src/lib/float.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
fn float_to_str(num: float, digits: uint) -> str {
/**
* String conversions
*/

fn to_str(num: float, digits: uint) -> str {
let accum = if num < 0.0 { num = -num; "-" } else { "" };
let trunc = num as uint;
let frac = num - (trunc as float);
Expand All @@ -15,21 +19,180 @@ fn float_to_str(num: float, digits: uint) -> str {
ret accum;
}

fn str_to_float(num: str) -> float {
let digits = str::split(num, '.' as u8);
let total = int::from_str(digits[0]) as float;
/**
* Convert a string to a float
*
* This function accepts strings such as
* * "3.14"
* * "+3.14", equivalent to "3.14"
* * "-3.14"
* * "2.5E10", or equivalently, "2.5e10"
* * "2.5E-10"
* * "", or, equivalently, "." (understood as 0)
* * "5."
* * ".5", or, equivalently, "0.5"
*
* @param num A string, possibly empty.
* @return [NaN] if the string did not represent a valid number.
* @return Otherwise, the floating-point number represented [num].
*/
fn from_str(num: str) -> float {
let pos = 0u; //Current byte position in the string.
//Used to walk the string in O(n).
let len = str::byte_len(num); //Length of the string, in bytes.

fn dec_val(c: char) -> int { ret (c as int) - ('0' as int); }
if len == 0u { ret 0.; }
let total = 0f; //Accumulated result
let c = 'z'; //Latest char.

let right = digits[1];
let len = str::char_len(digits[1]);
let i = 1u;
while (i < len) {
total += dec_val(str::pop_char(right)) as float /
(int::pow(10, i) as float);
i += 1u;
}
ret total;
//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
let neg = false; //Sign of the result
alt str::char_at(num, 0u) {
'-' {
neg = true;
pos = 1u;
}
'+' {
pos = 1u;
}
_ {}
}

//Examine the following chars until '.', 'e', 'E'
while(pos < len) {
let char_range = str::char_range_at(num, pos);
c = char_range.ch;
pos = char_range.next;
alt c {
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' {
total = total * 10f;
total += ((c as int) - ('0' as int)) as float;
}
_ {
break;
}
}
}

if c == '.' {//Examine decimal part
let decimal = 1.f;
while(pos < len) {
let char_range = str::char_range_at(num, pos);
c = char_range.ch;
pos = char_range.next;
alt c {
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' {
decimal /= 10.f;
total += (((c as int) - ('0' as int)) as float)*decimal;
}
_ {
break;
}
}
}
}

if (c == 'e') | (c == 'E') {//Examine exponent
let exponent = 0u;
let neg_exponent = false;
if(pos < len) {
let char_range = str::char_range_at(num, pos);
c = char_range.ch;
alt c {
'+' {
pos = char_range.next;
}
'-' {
pos = char_range.next;
neg_exponent = true;
}
_ {}
}
while(pos < len) {
let char_range = str::char_range_at(num, pos);
c = char_range.ch;
pos = char_range.next;
alt c {
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' {
exponent *= 10u;
exponent += ((c as uint) - ('0' as uint));
}
_ {
break;
}
}
}
let multiplier = pow_uint_to_uint_as_float(10u, exponent);
//Note: not [int::pow], otherwise, we'll quickly
//end up with a nice overflow
if neg_exponent {
total = total / multiplier;
} else {
total = total * multiplier;
}
}
}

if(pos < len) {
ret NaN();
} else {
if(neg) {
total *= -1f;
}
ret total;
}
}

/**
* Arithmetics
*/

/**
* Compute the exponentiation of an integer by another integer as a float.
*
*
* @param x The base.
* @param pow The exponent.
* @return [NaN] of both [x] and [pow] are [0u], otherwise [x^pow].
*/
fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
if x == 0u {
if pow == 0u {
ret NaN();
}
ret 0.;
}
let my_pow = pow;
let total = 1f;
let multiplier = x as float;
while (my_pow > 0u) {
if my_pow % 2u == 1u {
total = total * multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
ret total;
}


/**
* Constants
*/

//TODO: Once this is possible, replace the body of these functions
//by an actual constant.

fn NaN() -> float {
ret 0./0.;
}

fn infinity() -> float {
ret 1./0.;
}

fn neg_infinity() -> float {
ret -1./0.;
}

//
Expand Down
23 changes: 13 additions & 10 deletions src/lib/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ fn to_str(n: int, radix: uint) -> str {
fn str(i: int) -> str { ret to_str(i, 10u); }

fn pow(base: int, exponent: uint) -> int {
ret if exponent == 0u {
1
} else if base == 0 {
0
} else {
let accum = base;
let count = exponent;
while count > 1u { accum *= base; count -= 1u; }
accum
};
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
if base == 0 { ret 0; }
let my_pow = exponent;
let acc = 1;
let multiplier = base;
while(my_pow > 0u) {
if my_pow % 2u == 1u {
acc *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
ret acc;
}
// Local Variables:
// mode: rust;
Expand Down
19 changes: 19 additions & 0 deletions src/test/stdtest/float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std;
import std::float;

#[test]
fn test_from_str() {
assert ( float::from_str("3.14") == 3.14 );
assert ( float::from_str("+3.14") == 3.14 );
assert ( float::from_str("-3.14") == -3.14 );
assert ( float::from_str("2.5E10") == 25000000000. );
assert ( float::from_str("2.5e10") == 25000000000. );
assert ( float::from_str("25000000000.E-10") == 2.5 );
assert ( float::from_str("") == 0. );
assert ( float::from_str(" ") == 0. );
assert ( float::from_str(".") == 0. );
assert ( float::from_str("5.") == 5. );
assert ( float::from_str(".5") == 0.5 );
assert ( float::from_str("0.5") == 0.5 );

}
1 change: 1 addition & 0 deletions src/test/stdtest/stdtest.rc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod sys;
mod task;
mod test;
mod uint;
mod float;

// Local Variables:
// mode: rust
Expand Down