x raised to the power 0 does not evaluate to 1
I created a class for doing unit conversions, specifically bytes to
kilobytes, megabytes, gigabytes, etc. I have an enum with B through PB,
but for some reason 1024^0 does not return 1 and it's not correctly
converting from bytes to bytes or bytes to kilobytes, etc.
Here's my class:
public static class UnitConversion
{
/// <summary>
/// 1024^n
/// </summary>
public enum ByteConversionConstant
{
B = 0,
KB = 1,
MB = 2,
GB = 3,
TB = 4,
PB = 5
}
public static string GetValueFromBytes(long bytes,
ByteConversionConstant constant)
{
int n = (int)constant;
long divisor = 1024^n;
return (bytes / divisor).ToString() +
Enum.GetName(typeof(ByteConversionConstant), constant);
}
}
The following statement should return exactly the same value as
fileInfo.Length, but since 1024^0 isn't returning 1, it's showing the
number of kilobytes. Note, I had the GetValueFromBytes method all in one
line, but I separated it out to see what could be causing the
miscalculation.
UnitConversion.GetValueFromBytes(fileInfo.Length,
UnitConversion.ByteConversionConstant.B)
I'm not sure if it's an issue with casting the enum to an int or if
something gets lost when raising an int to an int and assigning it to a
long, but this is strange behavior.
No comments:
Post a Comment