3 ms·
If I run this in C#; it truncates to 6 and not 7... does C do an implicit round? Broken down, you get this: int toTest = 128; var div
by NKCSS 9y ago
If I run this in C#; it truncates to 6 and not 7... does C do an implicit round?
Broken down, you get this:
int toTest = 128;
var div = toTest / 3;
var sqrt = Math.Sqrt(div);
var logSqrt = Math.Log(sqrt);
var log2 = Math.Log(2);
var logDiv = logSqrt / log2;
var pow = Math.Pow(2, logDiv);
var result = pow * pow * 3;
int result2 = (int)Math.Pow(2, Math.Log(Math.Sqrt(128 / 3)) / Math.Log(2));
int total = result2 * result2 * 3;
this {Clusters.DAL.Tests.UtilitiesTests} Clusters.DAL.Tests.UtilitiesTests
toTest 128 int
div 42 int
sqrt 6.48074069840786 double
logSqrt 1.8688348091416842 double
log2 0.69314718055994529 double
logDiv 2.6961587113893803 double
pow 6.4807406984078613 double
result 126.00000000000004 double
result2 6 int
total 108 int
- NKCSS 9y agoWouldn't this be easier to understand for newbies? [TestMethod] public void NextPowerOfTwo() { const int bitPowers = 32; const int StartValue = 2; const int PowerOf = 2; UInt32[] powers = new UInt32[bitPowers - 1]; UInt32 value = StartValue; int i = 0; powers[i++] = value; for(; i < powers.Length; i++) { value *= PowerOf; powers[i] = value; } int[] valuesToTest = new int[] { 126, 128, 130 }; int power; foreach(int valueToTest in valuesToTest) { power = 0; value = powers[power]; while(valueToTest > value) { power++; value = powers[power]; } System.Diagnostics.Debug.WriteLine($"{valueToTest} -> {value}"); } } Test Name: NextPowerOfTwo Test Outcome: Passed Result StandardOutput: Debug Trace: 126 -> 128 128 -> 128 130 -> 256
- quincunx 9y agotoTest should be 128 * 128 * 3 to reflect 128 by 128 pixels of 3 bytes each, as would be the file size. edit: just to add, I think "width = sqrt(filesize/3)" would be a better version for newbies. Even better would be to replace the code altogether.
- NKCSS 9y agoI found what was causing the off-result for me; if you do: 128 / 3 (2 int's) in C# it will truncate to int; if you do 128 / 3.0 you'll get the expected output and everything starts to make sense :)
- quincunx 9y agook, two other things that may be throwing you off, the input is a filesize, not a width, the result of filesize / 3 is always an integer (even if float representation,) and the result of sqrt(filesize/3) is also always an integer (even if float representation,) as per the comments. Also, taking the 2log and then 2power is a no-op because it doesn't truncate / ceiling in-between the two. You kind of see where the code was going to, but it never quite made it there.