I really enjoyed the Unity explanation, it’s nice to see how other APIs handle the same things. UE4 doesn’t seem to be that different from the way Unity does it.
public void Start()
{
int baseNum = 2;
int exponent = 24;
CalculatePower(baseNum, exponent);
}
public void CalculatePower(double numBase, double exponent)
{
Debug.Log(numBase + " raised to the power of " + exponent + " is " +
System.Math.Pow(numBase, exponent));
}
If you’re only using whole numbers you can store the result in an int, so that’s absolutely fine in this case.
However, if you’re number or exponent include decimals then you should use a float or double, depending on the level of precision required (a float is fine 99.9% of the time) .
Great work @David2.
The number isn’t too big. Remember that the E+07 is just telling you to move the decimal place 7 spaces to the right - So the full number would be 16,777,220.
I’ll definitely be taking advantage of using System.Math.Pow vs. Mathf.Pow from now on though, as I’m not much one for scientific notation if I don’t need it ha! That was a great bit of clarity that I’m sure I would’ve been scouring to find at some later date.
Here’s my attemp. I was curious how fast is Math.Pow vs simple for loop based multiplication, so I did few benchmarks using BenchmarkDotNet.
Ryzen 2600x, Windows 10.0.19043 Build 19043, 16GB Ram ~3000MHz, and .NET6
Main.cs
using BenchmarkDotNet.Running;
using math_part_2;
//question
//Console.WriteLine(Math.Pow(2, 24));
//some tests
//var benchmarks = new SquaringBenchmarks();
//Console.WriteLine(benchmarks.MathPow());
//Console.WriteLine(benchmarks.ForPow());
//benchmarks!
BenchmarkRunner.Run<SquaringBenchmarks>();
Benchmarked code
public class SquaringBenchmarks
{
[Benchmark]
public double ForPow()
{
var number = 2;
for (int i = 1; i < 24; i++)
{
number *= 2;
}
return number;
}
[Benchmark]
public double MathPow()
{
return Math.Pow(2, 24);
}
}