1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

Scientific notation support

This commit is contained in:
Brian Fiete 2021-07-02 11:20:51 -07:00
parent 34c444767d
commit 1efdb334b4
2 changed files with 37 additions and 0 deletions

View file

@ -175,6 +175,17 @@ namespace System
{
char8 c = val.Ptr[i];
//Exponent prefix used in scientific notation. E.g. 1.2E5
if ((c == 'e') || (c == 'E'))
{
//Error if there are no numbers after the prefix
if(i == val.Length - 1)
return .Err;
var exponent = Try!(int32.Parse(val.Substring(i + 1)));
result *= Math.Pow(10, (double)exponent);
break;
}
if (c == '.')
{
if (decimalMultiplier != 0)

View file

@ -0,0 +1,26 @@
using System;
namespace Tests
{
class Floats
{
public static mixin FloatParseTest(StringView string, float expectedResult)
{
float result = float.Parse(string);
Test.Assert(expectedResult == result);
}
[Test]
public static void TestBasics()
{
FloatParseTest!("1.2", 1.2f);
FloatParseTest!("-0.2", -0.2f);
FloatParseTest!("2.5E2", 2.5E2f);
FloatParseTest!("2.7E-10", 2.7E-10f);
FloatParseTest!("-0.17E-7", -0.17E-7f);
FloatParseTest!("8.7e6", 8.7e6f);
FloatParseTest!("3.3e-11", 3.3e-11f);
FloatParseTest!("0.002e5", 0.002e5f);
}
}
}