Ln(x) and Sqrtaï(x) function added

This commit is contained in:
Nitload
2023-01-23 20:38:57 +08:00
parent c14b7ae7f8
commit 4c5dfb4828
39 changed files with 254 additions and 40 deletions
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -5,7 +5,7 @@ VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharp", "StgSharp\StgSharp.csproj", "{EEC1D075-F8F4-43EA-914A-02B1339D2305}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StgSharpDebug", "StgSharpDebug\StgSharpDebug.csproj", "{467F2A1A-D7E2-4904-AAAD-A5998EE45C71}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharpDebug", "StgSharpDebug\StgSharpDebug.csproj", "{467F2A1A-D7E2-4904-AAAD-A5998EE45C71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
+48 -2
View File
@@ -1,11 +1,35 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using static System.Net.WebRequestMethods;
namespace StgSharp
{
public static partial class Math
{
static private readonly float[] logArray =
{
0f, 0.00995033085316809f, 0.0198026272961797f, 0.0295588022415444f, 0.0392207131532813f, 0.048790164169432f, 0.0582689081239758f, 0.0676586484738149f, 0.0769610411361284f, 0.0861776962410524f,
0.0953101798043249f, 0.104360015324243f, 0.113328685307003f, 0.122217632724249f, 0.131028262406404f, 0.139761942375159f, 0.148420005118273f, 0.157003748809665f, 0.165514438477573f, 0.173953307123438f,
0.182321556793955f, 0.19062035960865f, 0.198850858745165f, 0.207014169384326f, 0.215111379616945f, 0.22314355131421f, 0.231111720963387f, 0.2390169004705f, 0.246860077931526f, 0.254642218373581f,
0.262364264467491f, 0.27002713721306f, 0.27763173659828f, 0.285178942233662f, 0.29266961396282f, 0.300104592450338f, 0.307484699747961f, 0.314810739840034f, 0.322083499169113f, 0.3293037471426f,
0.336472236621213f, 0.343589704390077f, 0.350656871613169f, 0.357674444271816f, 0.364643113587909f, 0.371563556432483f, 0.378436435720245f, 0.385262400790645f, 0.392042087776024f, 0.398776119957368f,
0.405465108108164f, 0.412109650826833f, 0.418710334858185f, 0.425267735404344f, 0.431782416425538f, 0.438254930931155f, 0.444685821261446f, 0.451075619360217f, 0.457424847038875f, 0.46373401623214f,
0.470003629245736f, 0.476234178996372f, 0.482426149244293f, 0.488580014818671f, 0.494696241836107f, 0.500775287912489f, 0.506817602368452f, 0.512823626428664f, 0.518793793415168f, 0.524728528934982f,
0.53062825106217f, 0.536493370514568f, 0.542324290825362f, 0.548121408509688f, 0.553885113226438f, 0.559615787935423f, 0.56531380905006f, 0.570979546585738f, 0.576613364303994f, 0.582215619852664f,
0.587786664902119f, 0.593326845277734f, 0.598836501088704f, 0.60431596685333f, 0.609765571620894f, 0.615185639090233f, 0.62057648772511f, 0.625938430866495f, 0.631271776841858f, 0.636576829071551f,
0.641853886172395f, 0.647103242058538f, 0.65232518603969f, 0.657520002916794f, 0.662687973075237f, 0.667829372575655f, 0.672944473242426f, 0.678033542749897f, 0.683096844706444f, 0.688134638736401f,
};
/// <summary>
///
/// </summary>
@@ -13,9 +37,31 @@ namespace StgSharp
/// <returns>
/// Returns the value of ln(x).
/// </returns>
public static float Log(float x)
public unsafe static float Log(float x)
{
uint input = *(uint*)&x;
uint f = input;
f >>= 23;
f -= 127;
input <<= 9;
input >>= 9;
input += 0b_0011_1111_1000_0000_0000_0000_0000_0000;
float result = *(float*)&input;
int order = (int)(result * 100)-100;
float cache = (result) / (1f + 0.01f * order) -1 ;
result = cache * -0.25f + 0.3333333333333f;
result = result * cache - 0.5f;
result = result * cache + 1.0f;
result *= cache;
return
f*0.6931471806f +
result +
logArray[order];
}
}
}
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace StgSharp
{
public static partial class Math
{
/*本方法将小数部分统一提升至2^16处后进行线性近似
const uint sqmov = 0b_0100_0111_1000_0000_0000_0000_0000_0000;
const float sqmov1 = 0.0015947f;
const float sqmov2 = 156.7673435f;
const float sqmov3 = 261.2776027f;
/**/
public static unsafe float Sqrt(float x)
{
/*
* 本方法采用分别对指数和小数处理的方法计算平方根
* 将指数除以2即可获得结果的指数
* (补充:指数最低位被加到小数位中以抵消一个多余的2 ^ 1的影响)
* 小数部分则统一乘以2 ^ 16后由y = Sqrt(x)在1.5 * 2 ^ 16处的切线近似
* 为简化操作,提高计算速度,切线左界被移动到远点
* 在进一步简化操作后,计算精度达到0.5%,速度约为.Net库函数的1.8倍
*/
/*
* 提取指数位并减去指数位中的127
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
uint a = *(uint*)&x;
uint f = a;
f >>= 23;
f /= 2;
f += 64;
/*
* 提取小数位
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
a -= 0b_0011_1111_0000_0000_0000_0000_0000_0000;
uint e = a << 8;
e >>= 8;
/*线性近似*/
e *= 0b_1101_0001;
e >>= 24;
e += 0b_0000_0000_0001_0000_1010_1000_1110_0010;
/*还原指数*/
f <<= 23;
e += f;
float result = *(float*)&e;
result = (result + x / result) / 2;
return (result + x / result) / 2;
}
}
}
@@ -4,5 +4,14 @@
<name>StgSharp</name>
</assembly>
<members>
<member name="M:StgSharp.Math.Log(System.Single)">
<summary>
</summary>
<param name="x"></param>
<returns>
Returns the value of ln(x).
</returns>
</member>
</members>
</doc>
@@ -1 +1 @@
ffe0859de4666337fddb22395a629f5b2a7223e3
9f6c1da9ad577916e70408e10ff0292581a33dcb
@@ -4,5 +4,14 @@
<name>StgSharp</name>
</assembly>
<members>
<member name="M:StgSharp.Math.Log(System.Single)">
<summary>
</summary>
<param name="x"></param>
<returns>
Returns the value of ln(x).
</returns>
</member>
</members>
</doc>
+60 -36
View File
@@ -1,54 +1,78 @@
using StgSharp;
using System;
using System.Diagnostics;
internal class Program
{
private static void Main(string[] args)
private unsafe static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
a asd = new a();
ase a01 = new ase(asd.cwc);
ase a02 = a01;
a01 -= asd.cwc;
a01 += asd.cwb;
a01.Invoke();
a02.Invoke();
a fff = new a();
fff._a = 10;
a ggg = fff;
fff._a = 15;
Console.WriteLine(ggg._a);
Point p = new Point();
Point p2 = new Point();
ContainerNode<Point> pC = new ContainerNode<Point>(p);
ContainerNode<Point> pC02 = new ContainerNode<Point>(p2);
pC.Next=pC02;
/*
int cc = 3;
Console.WriteLine(cc/2);
/**/
/*
float aa = 261.2776027f;
uint bb = *(uint*)&aa;
Console.WriteLine(Convert.ToString(bb,toBase:2));
Console.WriteLine(StgSharp.Math.Log(3.5f));
/**/
for (int wasd = 0; wasd < 100; wasd++)
{
float time = 1000000;
double a = 114514;
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // 开始监视代码运行时间
for (float i = 2; i < time; i++)
{
a = StgSharp.Math.Log(i);
}
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
double milliseconds = timespan.TotalMilliseconds; // 总毫秒数
Console.WriteLine($"{milliseconds},{a}");
System.Diagnostics.Stopwatch stopwatch1 = new Stopwatch();
double b = 114514;
double time2 = time;
stopwatch1.Start();
for (double i = 2; i < time2; i++)
{
b = System.Math.Log(i);
}
stopwatch1.Stop();
timespan = stopwatch1.Elapsed;
double milliseconds2 = timespan.TotalMilliseconds;
Console.WriteLine($"{milliseconds2},{b}");
Console.WriteLine("");
}
/**/
/*
double accuracy = 0;
for (float i = 1.001f; i < 4; i += 0.001f)
{
double r1 = StgSharp.Math.Log(i);
double r2 = System.Math.Log(i);
double ac = ( System.Math.Max(r1,r2)- System.Math.Min(r1, r2) ) / System.Math.Max(r1, r2);
if (ac>accuracy)
{
accuracy = ac;
Console.WriteLine(i);
Console.WriteLine(ac);
Console.WriteLine(r1);
Console.WriteLine(r2);
Console.WriteLine("");
}
}
Console.WriteLine(accuracy);
/**/
}
public delegate void ase();
}
class a
{
public int _a;
public void cwc()
{
Console.WriteLine("c");
}
public void cwb()
{
Console.WriteLine("b");
}
}
@@ -5,6 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
@@ -4,5 +4,14 @@
<name>StgSharp</name>
</assembly>
<members>
<member name="M:StgSharp.Math.Log(System.Single)">
<summary>
</summary>
<param name="x"></param>
<returns>
Returns the value of ln(x).
</returns>
</member>
</members>
</doc>
+31
View File
@@ -0,0 +1,31 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>E439935D-B2A2-46AE-856D-2AA408428F33</ProjectGuid>
<!--457858db-8f6d-45ae-8571-166818eeb6f7-->
<ProjectHome>.</ProjectHome>
<StartupFile>
</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>calc</Name>
<RootNamespace>calc</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="file1.lua">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
</Project>
+20
View File
@@ -0,0 +1,20 @@
--region *.lua
--Date
--This file is automatically generated by the 【BabeLua】 plugin
a = 1;
b = 0.001;
c = 0;
while a-b > 0.0001*a do
c= (a+b)/2
b = math.sqrt( a*b )
a = c;
end
print(a,b)
--endregion