| 1 | use std::str::FromStr; |
| 2 | |
| 3 | use hexf_parse::{parse_hexf32, parse_hexf64}; |
| 4 | |
| 5 | fn hexf32(s: &str) -> f32 { |
| 6 | parse_hexf32(s, false).unwrap() |
| 7 | } |
| 8 | |
| 9 | fn hexf64(s: &str) -> f64 { |
| 10 | parse_hexf64(s, false).unwrap() |
| 11 | } |
| 12 | |
| 13 | macro_rules! check { |
| 14 | ($ty:ident, $s:expr) => {{ |
| 15 | check!($ty, stringify!($s), $s) |
| 16 | }}; |
| 17 | ($ty:ident, $s:expr,inf) => {{ |
| 18 | check!($ty, $s, core::$ty::INFINITY) |
| 19 | }}; |
| 20 | ($ty:ident, $s:expr,neg_inf) => {{ |
| 21 | check!($ty, $s, core::$ty::NEG_INFINITY) |
| 22 | }}; |
| 23 | ($ty:ident, $s:expr, $e:expr) => {{ |
| 24 | let string = String::from($s); |
| 25 | let s = string.as_bytes(); |
| 26 | let expected: $ty = $e; |
| 27 | let result = fast_float2::parse::<$ty, _>(s).unwrap(); |
| 28 | assert_eq!(result, expected); |
| 29 | let lex = lexical_core::parse::<$ty>(s).unwrap(); |
| 30 | assert_eq!(result, lex); |
| 31 | let std = <$ty>::from_str(string.as_str()); |
| 32 | if let Ok(std) = std { |
| 33 | // stdlib can't parse all weird floats |
| 34 | if std.is_finite() && result.is_finite() { |
| 35 | // some weird edge cases stdlib parses as inf, e.g. 0e999999999999999 |
| 36 | assert_eq!(result, std); |
| 37 | } |
| 38 | } |
| 39 | }}; |
| 40 | } |
| 41 | macro_rules! check_lex { |
| 42 | ($ty:ident, $s:expr) => {{ |
| 43 | let v = lexical_core::parse::<$ty>($s.as_bytes()).unwrap(); |
| 44 | check!($ty, $s, v); |
| 45 | }}; |
| 46 | } |
| 47 | macro_rules! check_f32 { |
| 48 | ($($tt:tt)+) => { |
| 49 | check!(f32, $($tt)+) |
| 50 | } |
| 51 | } |
| 52 | macro_rules! check_f64 { |
| 53 | ($($tt:tt)+) => { |
| 54 | check!(f64, $($tt)+) |
| 55 | } |
| 56 | } |
| 57 | macro_rules! check_f32_lex { |
| 58 | ($s:expr) => { |
| 59 | check_lex!(f32, $s) |
| 60 | }; |
| 61 | } |
| 62 | macro_rules! check_f64_lex { |
| 63 | ($s:expr) => { |
| 64 | check_lex!(f64, $s) |
| 65 | }; |
| 66 | } |
| 67 | macro_rules! check_f32_inf { |
| 68 | ($s:expr) => { |
| 69 | check!(f32, $s, inf) |
| 70 | }; |
| 71 | } |
| 72 | macro_rules! check_f32_neg_inf { |
| 73 | ($s:expr) => { |
| 74 | check!(f32, $s, neg_inf) |
| 75 | }; |
| 76 | } |
| 77 | macro_rules! check_f64_inf { |
| 78 | ($s:expr) => { |
| 79 | check!(f64, $s, inf) |
| 80 | }; |
| 81 | } |
| 82 | macro_rules! check_f64_neg_inf { |
| 83 | ($s:expr) => { |
| 84 | check!(f64, $s, neg_inf) |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | fn append_zeros(s: impl AsRef<str>, n: usize) -> String { |
| 89 | let mut s = String::from(s.as_ref()); |
| 90 | for _ in 0..n { |
| 91 | s.push('0'); |
| 92 | } |
| 93 | s |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_f64_inf() { |
| 98 | check_f64_inf!("INF"); |
| 99 | check_f64_inf!("INFINITY"); |
| 100 | check_f64_inf!("infinity"); |
| 101 | check_f64_inf!("inf"); |
| 102 | check_f64_inf!("1234456789012345678901234567890e9999999999999999999999999999"); |
| 103 | check_f64_inf!("1.832312213213213232132132143451234453123412321321312e308"); |
| 104 | check_f64_inf!("2e30000000000000000"); |
| 105 | check_f64_inf!("2e3000"); |
| 106 | check_f64_inf!("1.8e308"); |
| 107 | check_f64_inf!("1.9e308"); |
| 108 | |
| 109 | check_f64_neg_inf!("-INF"); |
| 110 | check_f64_neg_inf!("-INFINITY"); |
| 111 | check_f64_neg_inf!("-infinity"); |
| 112 | check_f64_neg_inf!("-inf"); |
| 113 | check_f64_neg_inf!("-2139879401095466344511101915470454744.9813888656856943E+272"); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn test_f64_long() { |
| 118 | check_f64!( |
| 119 | "\ |
| 120 | 9355950000000000000.000000000000000000000000000000000018446744073709551616000001\ |
| 121 | 84467440737095516161844674407370955161407370955161618446744073709551616000184467\ |
| 122 | 44073709551616600000184467440737095516161844674407370955161407370955161618446744\ |
| 123 | 07370955161600018446744073709551616018446744073709556744516161844674407370955161\ |
| 124 | 40737095516161844674407370955161600018446744073709551616018446744073709551611616\ |
| 125 | 00018446744073709500184467440737095516160018446744073709551616001844674407370955\ |
| 126 | 11681644674407370955161600018440737095516160184467440737095516161844674407370955\ |
| 127 | 16160001844674407536910751601611616000184467440737095001844674407370955161600184\ |
| 128 | 46744073709551616001844674407370955161618446744073709551616000184495516161844674\ |
| 129 | 4073709551616000184467440753691075160018446744073709", |
| 130 | hexf64("0x1.03ae05e8fca1cp+63") |
| 131 | ); |
| 132 | check_f64!( |
| 133 | "\ |
| 134 | 2.225073858507202124188701479202220329072405282794390378143031338374351073192441\ |
| 135 | 94686754406432563881851382188218502438069999947733013005649884107791928741341929\ |
| 136 | 29720097048195199306799329096904278406473168204156592672863293363047467012331685\ |
| 137 | 29834221527445172608358596545663192828352447877877998943107797838336991592885945\ |
| 138 | 55213714181128458251145584319223079897504395086859412457230891738946169368372321\ |
| 139 | 19137365897797772328669884035639025104444303545739673370658398105542045669382465\ |
| 140 | 84137476071559811765738776267476659123871999319040063173347090030127901881752034\ |
| 141 | 47190250028061277777916798391090578584006464715943810511489154282775041174682194\ |
| 142 | 13395246668250343130618158782937900420539237507208336669324158000275839111885418\ |
| 143 | 8641513168478436313080237596295773983001708984375e-308", |
| 144 | hexf64("0x1.0000000000002p-1022") |
| 145 | ); |
| 146 | check_f64_inf!( |
| 147 | "\ |
| 148 | 14384566631413902735261182076422355811832278452463312311626366537903681520913941\ |
| 149 | 96930365828634687637948157940776599182791387527135353034738357134110310609455693\ |
| 150 | 90082419354977279201654318268051974058035436546798544018359870131225762454556233\ |
| 151 | 13970183299286131961255902741877200739148180625308303165331580986249841188892982\ |
| 152 | 81371812288789537310599037529113415438738954894752124724983067241108764488346454\ |
| 153 | 37669901867307840475112141480493722424080599312381693232622368309077056159757045\ |
| 154 | 77939329858261626042558845291341263962822021265262533893834218067279545885255961\ |
| 155 | 14379801269094096329805054803089299736996870951258573010877404407451953846698609\ |
| 156 | 19821392688269207855703322826525930548119852605981316446918758669325733577952202\ |
| 157 | 04076454986842633399219052275566166981299674128912822316855046606712779271982900\ |
| 158 | 09824680186319750978665734576683784255802269708917361719466043175201158849097881\ |
| 159 | 37047711185017157986905601606166617302905958843377601564443970505037755427769614\ |
| 160 | 39282780934537928038462527159660167332226464423828921239400524413468224297215938\ |
| 161 | 84378212558701004356924243030059517489346646577724622498919752597382095222500311\ |
| 162 | 12418182351225107135618176937657765139002829779615620881537508915912839494571051\ |
| 163 | 58613344862671017974971111259092725051947928708896171797587034426080161433432621\ |
| 164 | 59998149700606597792535574457560429226974273443630323818747730771316763398572110\ |
| 165 | 87495998192373246307688452867739265415001026982223940199342748237651323138921235\ |
| 166 | 35835735663769155726509168665536123661873789595549835667127670933729060301889762\ |
| 167 | 20169058025354973622211666504549316958271880975697143546564469806791358707318873\ |
| 168 | 07570838334500409015197406832583817753126695417740666139222980134999469594150993\ |
| 169 | 5655355652985723782153570084089560139142231.738475042362596875449154552392299548\ |
| 170 | 94713816208169416867534067784380761312978044932336375902701297246698737092181681\ |
| 171 | 31626587547265451210905455072402670004565947865409496052607224619378706306348749\ |
| 172 | 91729398208026467698131898691830012167897399682179601734569071423681e-733" |
| 173 | ); |
| 174 | check_f64_lex!( |
| 175 | "\ |
| 176 | 0.000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 177 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 178 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 179 | 00000000000000000000000000000000000000000000000000000000000000000000044501477170\ |
| 180 | 14402272114819593418263951869639092703291296046852219449644444042153891033059047\ |
| 181 | 81627017582829831782607924221374017287738918929105531441481564124348675997628212\ |
| 182 | 65346585071045737627442980259622449029037796981144446145705102663115100318287949\ |
| 183 | 52795966823603998647925096578034214163701381261333311989876551545144031526125381\ |
| 184 | 32666529513060001849177663286607555958373922409899478075565940981010216121988146\ |
| 185 | 05258742579179000071675999344145086087205681577915435923018910334964869420614052\ |
| 186 | 18289243144579760516365090360651414037721744226256159024466852576737244643007551\ |
| 187 | 33324500796506867194913776884780053099639677097589658441378944337966219939673169\ |
| 188 | 36280457084866613206797017728916080020698679408551343728867675409720757232455434\ |
| 189 | 770912461317493580281734466552734375" |
| 190 | ); |
| 191 | check_f64_lex!( |
| 192 | "\ |
| 193 | 0.000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 194 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 195 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000\ |
| 196 | 00000000000000000000000000000000000000000000000000000000000000000000022250738585\ |
| 197 | 07200889024586876085859887650423112240959465493524802562440009228235695178775888\ |
| 198 | 80375915526423097809504343120858773871583572918219930202943792242235598198275012\ |
| 199 | 42041788969571311791082261043971979604000454897391938079198936081525613113376149\ |
| 200 | 84204327175103362739154978273159414382813627511383860409424946494228631669542910\ |
| 201 | 50802018159266421349966065178030950759130587198464239060686371020051087232827846\ |
| 202 | 78843631944515866135041223479014792369585208321597621066375401613736583044193603\ |
| 203 | 71477835530668283453563400507407304013560296804637591858316312422452159926254649\ |
| 204 | 43008368518617194224176464551371354201322170313704965832101546540680353974179060\ |
| 205 | 22589503023501937519773030945763173210852507299305089761582519159720757232455434\ |
| 206 | 770912461317493580281734466552734375" |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn test_f64_general() { |
| 212 | check_f64!("9007199254740993.0", hexf64("0x1.p+53")); |
| 213 | check_f64!(append_zeros("9007199254740993.0", 1000), hexf64("0x1.p+53")); |
| 214 | check_f64!("10000000000000000000", hexf64("0x1.158e460913dp+63")); |
| 215 | check_f64!("10000000000000000000000000000001000000000000", hexf64("0x1.cb2d6f618c879p+142")); |
| 216 | check_f64!("10000000000000000000000000000000000000000001", hexf64("0x1.cb2d6f618c879p+142")); |
| 217 | check_f64!(1.1920928955078125e-07); |
| 218 | check_f64!("-0", -0.0); |
| 219 | check_f64!("1.0000000000000006661338147750939242541790008544921875", 1.0000000000000007); |
| 220 | check_f64!("1090544144181609348835077142190", hexf64("0x1.b8779f2474dfbp+99")); |
| 221 | check_f64!(2.2250738585072013e-308); |
| 222 | check_f64!(-92666518056446206563E3); |
| 223 | check_f64!(-92666518056446206563E3); |
| 224 | check_f64!(-42823146028335318693e-128); |
| 225 | check_f64!(90054602635948575728E72); |
| 226 | check_f64_lex!( |
| 227 | "\ |
| 228 | 1.000000000000001885589208702234638701745660206917535153946435506630705583683732\ |
| 229 | 21972569761144603605635692374830246134201063722058e-309" |
| 230 | ); |
| 231 | check_f64!("0e9999999999999999999999999999", 0.0); |
| 232 | check_f64!(-2402844368454405395.2); |
| 233 | check_f64!(2402844368454405395.2); |
| 234 | check_f64!(7.0420557077594588669468784357561207962098443483187940792729600000e+59); |
| 235 | check_f64!(7.0420557077594588669468784357561207962098443483187940792729600000e+59); |
| 236 | check_f64!(-1.7339253062092163730578609458683877051596800000000000000000000000e+42); |
| 237 | check_f64!(-2.0972622234386619214559824785284023792871122537545728000000000000e+52); |
| 238 | check_f64!(-1.0001803374372191849407179462120053338028379051879898808320000000e+57); |
| 239 | check_f64!(-1.8607245283054342363818436991534856973992070520151142825984000000e+58); |
| 240 | check_f64!(-1.9189205311132686907264385602245237137907390376574976000000000000e+52); |
| 241 | check_f64!(-2.8184483231688951563253238886553506793085187889855201280000000000e+54); |
| 242 | check_f64!(-1.7664960224650106892054063261344555646357024359107788800000000000e+53); |
| 243 | check_f64!(-2.1470977154320536489471030463761883783915110400000000000000000000e+45); |
| 244 | check_f64!(-4.4900312744003159009338275160799498340862630046359789166919680000e+61); |
| 245 | check_f64!("+1", 1.0); |
| 246 | check_f64!("1.797693134862315700000000000000001e308", 1.7976931348623157e308); |
| 247 | check_f64!("3e-324", hexf64("0x0.0000000000001p-1022")); |
| 248 | check_f64!("1.00000006e+09", hexf64("0x1.dcd651ep+29")); |
| 249 | check_f64!("4.9406564584124653e-324", hexf64("0x0.0000000000001p-1022")); |
| 250 | check_f64!("4.9406564584124654e-324", hexf64("0x0.0000000000001p-1022")); |
| 251 | check_f64!("2.2250738585072009e-308", hexf64("0x0.fffffffffffffp-1022")); |
| 252 | check_f64!("2.2250738585072014e-308", hexf64("0x1.p-1022")); |
| 253 | check_f64!("1.7976931348623157e308", hexf64("0x1.fffffffffffffp+1023")); |
| 254 | check_f64!("1.7976931348623158e308", hexf64("0x1.fffffffffffffp+1023")); |
| 255 | check_f64!(4503599627370496.5); |
| 256 | check_f64!(4503599627475352.5); |
| 257 | check_f64!(4503599627475353.5); |
| 258 | check_f64!(2251799813685248.25); |
| 259 | check_f64!(1125899906842624.125); |
| 260 | check_f64!(1125899906842901.875); |
| 261 | check_f64!(2251799813685803.75); |
| 262 | check_f64!(4503599627370497.5); |
| 263 | check_f64!(45035996.273704995); |
| 264 | check_f64!(45035996.273704985); |
| 265 | check_f64!(1.2345e30); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn test_f32_inf() { |
| 270 | check_f32_inf!("INF"); |
| 271 | check_f32_inf!("INFINITY"); |
| 272 | check_f32_inf!("infinity"); |
| 273 | check_f32_inf!("inf"); |
| 274 | check_f32_inf!("1234456789012345678901234567890e9999999999999999999999999999"); |
| 275 | check_f32_inf!("2e3000"); |
| 276 | check_f32_inf!("3.5028234666e38"); |
| 277 | |
| 278 | check_f32_neg_inf!("-INF"); |
| 279 | check_f32_neg_inf!("-INFINITY"); |
| 280 | check_f32_neg_inf!("-infinity"); |
| 281 | check_f32_neg_inf!("-inf"); |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn test_f32_basic() { |
| 286 | let f1 = "\ |
| 287 | 1.175494140627517859246175898662808184331245864732796240031385942718174675986064\ |
| 288 | 7699724722770042717456817626953125"; |
| 289 | check_f32!(f1, hexf32("0x1.2ced3p+0")); |
| 290 | check_f32!(format!("{}e-38", f1), hexf32("0x1.fffff8p-127")); |
| 291 | check_f32!(format!("{}e-38", append_zeros(f1, 655)), hexf32("0x1.fffff8p-127")); |
| 292 | check_f32!(format!("{}e-38", append_zeros(f1, 656)), hexf32("0x1.fffff8p-127")); |
| 293 | check_f32!(format!("{}e-38", append_zeros(f1, 1000)), hexf32("0x1.fffff8p-127")); |
| 294 | check_f32!(1.00000006e+09); |
| 295 | check_f32!(1.4012984643e-45); |
| 296 | check_f32!(1.1754942107e-38); |
| 297 | check_f32!(1.1754943508e-45); |
| 298 | check_f32!("-0", -0.0); |
| 299 | check_f32!("1090544144181609348835077142190", hexf32("0x1.b877ap+99")); |
| 300 | check_f32!(1.1754943508e-38); |
| 301 | check_f32!(30219.0830078125); |
| 302 | check_f32!(16252921.5); |
| 303 | check_f32!(5322519.25); |
| 304 | check_f32!(3900245.875); |
| 305 | check_f32!(1510988.3125); |
| 306 | check_f32!(782262.28125); |
| 307 | check_f32!(328381.484375); |
| 308 | check_f32!(156782.0703125); |
| 309 | check_f32!(85003.24609375); |
| 310 | check_f32!(43827.048828125); |
| 311 | check_f32!(17419.6494140625); |
| 312 | check_f32!(15498.36376953125); |
| 313 | check_f32!(6318.580322265625); |
| 314 | check_f32!(2525.2840576171875); |
| 315 | check_f32!(1370.9265747070312); |
| 316 | check_f32!(936.3702087402344); |
| 317 | check_f32!(411.88682556152344); |
| 318 | check_f32!(206.50310516357422); |
| 319 | check_f32!(124.16878890991211); |
| 320 | check_f32!(50.811574935913086); |
| 321 | check_f32!(17.486443519592285); |
| 322 | check_f32!(13.91745138168335); |
| 323 | check_f32!("7.5464513301849365", hexf32("0x1.e2f90ep+2")); |
| 324 | check_f32!(2.687217116355896); |
| 325 | check_f32!("1.1877630352973938", hexf32("0x1.30113ep+0")); |
| 326 | check_f32!(0.7622503340244293); |
| 327 | check_f32!("0.30531780421733856", hexf32("0x1.38a53ap-2")); |
| 328 | check_f32!("0.21791061013936996", hexf32("0x1.be47eap-3")); |
| 329 | check_f32!("0.09289376810193062", hexf32("0x1.7c7e2ep-4")); |
| 330 | check_f32!(0.03706067614257336); |
| 331 | check_f32!(0.028068351559340954); |
| 332 | check_f32!("0.012114629615098238", hexf32("0x1.8cf8e2p-7")); |
| 333 | check_f32!("0.004221370676532388", hexf32("0x1.14a6dap-8")); |
| 334 | check_f32!(0.002153817447833717); |
| 335 | check_f32!("0.0015924838953651488", hexf32("0x1.a175cap-10")); |
| 336 | check_f32!(0.0008602388261351734); |
| 337 | check_f32!("0.00036393293703440577", hexf32("0x1.7d9c82p-12")); |
| 338 | check_f32!(0.00013746770127909258); |
| 339 | check_f32!(16407.9462890625); |
| 340 | check_f32!("1.1754947011469036e-38", hexf32("0x1.000006p-126")); |
| 341 | check_f32!("7.0064923216240854e-46", hexf32("0x1.p-149")); |
| 342 | check_f32!(8388614.5); |
| 343 | check_f32!("0e9999999999999999999999999999", 0.); |
| 344 | check_f32!( |
| 345 | "4.7019774032891500318749461488889827112746622270883500860350068251e-38", |
| 346 | 4.7019774032891500318749461488889827112746622270883500860350068251e-38 |
| 347 | ); |
| 348 | check_f32_lex!( |
| 349 | "\ |
| 350 | 3.141592653589793238462643383279502884197169399375105820974944592307816406286208\ |
| 351 | 9986280348253421170679" |
| 352 | ); |
| 353 | check_f32!( |
| 354 | "2.3509887016445750159374730744444913556373311135441750430175034126e-38", |
| 355 | 2.3509887016445750159374730744444913556373311135441750430175034126e-38 |
| 356 | ); |
| 357 | check_f32!("+1", 1.); |
| 358 | check_f32!("7.0060e-46", 0.); |
| 359 | check_f32!("3.4028234664e38", hexf32("0x1.fffffep+127")); |
| 360 | check_f32!("3.4028234665e38", hexf32("0x1.fffffep+127")); |
| 361 | check_f32!("3.4028234666e38", hexf32("0x1.fffffep+127")); |
| 362 | check_f32_lex!( |
| 363 | "\ |
| 364 | 0.000000000000000000000000000000000000011754943508222875079687365372222456778186\ |
| 365 | 655567720875215087517062784172594547271728515625" |
| 366 | ); |
| 367 | check_f32_lex!( |
| 368 | "\ |
| 369 | 0.000000000000000000000000000000000000000000001401298464324817070923729583289916\ |
| 370 | 13128026194187651577175706828388979108268586060148663818836212158203125" |
| 371 | ); |
| 372 | check_f32_lex!( |
| 373 | "\ |
| 374 | 0.000000000000000000000000000000000000023509885615147285834557659820715330266457\ |
| 375 | 17985517980855365926236850006129930346077117064851336181163787841796875" |
| 376 | ); |
| 377 | check_f32_lex!( |
| 378 | "\ |
| 379 | 0.000000000000000000000000000000000000011754942106924410754870294448492873488270\ |
| 380 | 52428745893333857174530571588870475618904265502351336181163787841796875" |
| 381 | ); |
| 382 | check_f32!(1.2345e15); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn test_f64_pow10() { |
| 387 | for i in -308..=308 { |
| 388 | let s = format!("1e{}", i); |
| 389 | let v = f64::from_str(&s).unwrap(); |
| 390 | assert_eq!(fast_float2::parse::<f64, _>(s).unwrap(), v); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | #[test] |
| 395 | fn test_f32_pow10() { |
| 396 | for i in -38..=38 { |
| 397 | let s = format!("1e{}", i); |
| 398 | let v = f32::from_str(&s).unwrap(); |
| 399 | assert_eq!(fast_float2::parse::<f32, _>(s).unwrap(), v); |
| 400 | } |
| 401 | } |