Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason for mulitplying by an integer 'x', then dividing by that same 'x' as a double? [duplicate]

Tags:

I was running through some code the other day and found this line which I thought was peculiar.

Math.Round(20 * (bytes / 1024.0 / 1024.0 / 1024.0), MidpointRounding.AwayFromZero) / 20.0);

I can't understand why they are multiplying by int 20, and then dividing by float 20.0

My initial suspicion is that it had something to do with either rounding or precision, but I couldn't find the answer on here or on Google (at least not with my search terms). What is the purpose of doing this sort of operation?

edit

For context, this line of code is intended to convert the variable bytes into Gigabytes

like image 763
Sean Sailer Avatar asked Aug 28 '19 13:08

Sean Sailer


People also ask

What are the rules for multiplying and dividing integers?

RULE 1: The product of a positive integer and a negative integer is negative. RULE 2: The product of two positive integers is positive. RULE 3: The product of two negative integers is positive. RULE 1: The quotient of a positive integer and a negative integer is negative.

Why do we flip and multiply?

Since multiplying by 7 cancels division by 7, we may as well simply multiply by 4 (the divisor's numerator ). So, inverting and multiplying when dividing fractions is actually just a shortcut! Be sure to let your students know this; kids love shortcuts.

When multiplying integers with same signs then answer will be?

When you multiply two integers with the same signs, the result is always positive. Just multiply the absolute values and make the answer positive. When you multiply two integers with different signs, the result is always negative.


1 Answers

As Andrew Morton suggested in the comments, this will round to the next highest 0.05 GB increment -- here's some code and a fiddle to test it

float bytes;
for (bytes = 0; bytes < 1000000000; bytes += 5000000)
{
    var x = Math.Round(20 * (bytes / 1024.0 / 1024.0 / 1024.0), MidpointRounding.AwayFromZero) / 20.0;
    var y = Math.Round(bytes / 1024.0 / 1024.0 / 1024.0, MidpointRounding.AwayFromZero);
    Console.WriteLine("Bytes: {0} -- x: {1}, y: {2}", bytes, x , y);
}

produces the following output:


Bytes: 0 -- x: 0, y: 0
Bytes: 5000000 -- x: 0, y: 0
Bytes: 1E+07 -- x: 0, y: 0
Bytes: 1.5E+07 -- x: 0, y: 0
Bytes: 2E+07 -- x: 0, y: 0
Bytes: 2.5E+07 -- x: 0, y: 0
Bytes: 3E+07 -- x: 0.05, y: 0
Bytes: 3.5E+07 -- x: 0.05, y: 0
Bytes: 4E+07 -- x: 0.05, y: 0
Bytes: 4.5E+07 -- x: 0.05, y: 0
Bytes: 5E+07 -- x: 0.05, y: 0
Bytes: 5.5E+07 -- x: 0.05, y: 0
Bytes: 6E+07 -- x: 0.05, y: 0
Bytes: 6.5E+07 -- x: 0.05, y: 0
Bytes: 7E+07 -- x: 0.05, y: 0
Bytes: 7.5E+07 -- x: 0.05, y: 0
Bytes: 8E+07 -- x: 0.05, y: 0
Bytes: 8.5E+07 -- x: 0.1, y: 0
Bytes: 9E+07 -- x: 0.1, y: 0
Bytes: 9.5E+07 -- x: 0.1, y: 0
Bytes: 1E+08 -- x: 0.1, y: 0
Bytes: 1.05E+08 -- x: 0.1, y: 0
Bytes: 1.1E+08 -- x: 0.1, y: 0
Bytes: 1.15E+08 -- x: 0.1, y: 0
Bytes: 1.2E+08 -- x: 0.1, y: 0
Bytes: 1.25E+08 -- x: 0.1, y: 0
Bytes: 1.3E+08 -- x: 0.1, y: 0
Bytes: 1.35E+08 -- x: 0.15, y: 0
Bytes: 1.4E+08 -- x: 0.15, y: 0
Bytes: 1.45E+08 -- x: 0.15, y: 0
Bytes: 1.5E+08 -- x: 0.15, y: 0
Bytes: 1.55E+08 -- x: 0.15, y: 0
Bytes: 1.6E+08 -- x: 0.15, y: 0
Bytes: 1.65E+08 -- x: 0.15, y: 0
Bytes: 1.7E+08 -- x: 0.15, y: 0
Bytes: 1.75E+08 -- x: 0.15, y: 0
Bytes: 1.8E+08 -- x: 0.15, y: 0
Bytes: 1.85E+08 -- x: 0.15, y: 0
Bytes: 1.9E+08 -- x: 0.2, y: 0
Bytes: 1.95E+08 -- x: 0.2, y: 0
Bytes: 2E+08 -- x: 0.2, y: 0
Bytes: 2.05E+08 -- x: 0.2, y: 0
Bytes: 2.1E+08 -- x: 0.2, y: 0
Bytes: 2.15E+08 -- x: 0.2, y: 0
Bytes: 2.2E+08 -- x: 0.2, y: 0
Bytes: 2.25E+08 -- x: 0.2, y: 0
Bytes: 2.3E+08 -- x: 0.2, y: 0
Bytes: 2.35E+08 -- x: 0.2, y: 0
Bytes: 2.4E+08 -- x: 0.2, y: 0
Bytes: 2.45E+08 -- x: 0.25, y: 0
Bytes: 2.5E+08 -- x: 0.25, y: 0
Bytes: 2.55E+08 -- x: 0.25, y: 0
Bytes: 2.6E+08 -- x: 0.25, y: 0
Bytes: 2.65E+08 -- x: 0.25, y: 0
Bytes: 2.7E+08 -- x: 0.25, y: 0
Bytes: 2.75E+08 -- x: 0.25, y: 0
Bytes: 2.8E+08 -- x: 0.25, y: 0
Bytes: 2.85E+08 -- x: 0.25, y: 0
Bytes: 2.9E+08 -- x: 0.25, y: 0
Bytes: 2.95E+08 -- x: 0.25, y: 0
Bytes: 3E+08 -- x: 0.3, y: 0
Bytes: 3.05E+08 -- x: 0.3, y: 0
Bytes: 3.1E+08 -- x: 0.3, y: 0
Bytes: 3.15E+08 -- x: 0.3, y: 0
Bytes: 3.2E+08 -- x: 0.3, y: 0
Bytes: 3.25E+08 -- x: 0.3, y: 0
Bytes: 3.3E+08 -- x: 0.3, y: 0
Bytes: 3.35E+08 -- x: 0.3, y: 0
Bytes: 3.4E+08 -- x: 0.3, y: 0
Bytes: 3.45E+08 -- x: 0.3, y: 0
Bytes: 3.5E+08 -- x: 0.35, y: 0
Bytes: 3.55E+08 -- x: 0.35, y: 0
Bytes: 3.6E+08 -- x: 0.35, y: 0
Bytes: 3.65E+08 -- x: 0.35, y: 0
Bytes: 3.7E+08 -- x: 0.35, y: 0
Bytes: 3.75E+08 -- x: 0.35, y: 0
Bytes: 3.8E+08 -- x: 0.35, y: 0
Bytes: 3.85E+08 -- x: 0.35, y: 0
Bytes: 3.9E+08 -- x: 0.35, y: 0
Bytes: 3.95E+08 -- x: 0.35, y: 0
Bytes: 4E+08 -- x: 0.35, y: 0
Bytes: 4.05E+08 -- x: 0.4, y: 0
Bytes: 4.1E+08 -- x: 0.4, y: 0
Bytes: 4.15E+08 -- x: 0.4, y: 0
Bytes: 4.2E+08 -- x: 0.4, y: 0
Bytes: 4.25E+08 -- x: 0.4, y: 0
Bytes: 4.3E+08 -- x: 0.4, y: 0
Bytes: 4.35E+08 -- x: 0.4, y: 0
Bytes: 4.4E+08 -- x: 0.4, y: 0
Bytes: 4.45E+08 -- x: 0.4, y: 0
Bytes: 4.5E+08 -- x: 0.4, y: 0
Bytes: 4.55E+08 -- x: 0.4, y: 0
Bytes: 4.6E+08 -- x: 0.45, y: 0
Bytes: 4.65E+08 -- x: 0.45, y: 0
Bytes: 4.7E+08 -- x: 0.45, y: 0
Bytes: 4.75E+08 -- x: 0.45, y: 0
Bytes: 4.8E+08 -- x: 0.45, y: 0
Bytes: 4.85E+08 -- x: 0.45, y: 0
Bytes: 4.9E+08 -- x: 0.45, y: 0
Bytes: 4.95E+08 -- x: 0.45, y: 0
Bytes: 5E+08 -- x: 0.45, y: 0
Bytes: 5.05E+08 -- x: 0.45, y: 0
Bytes: 5.1E+08 -- x: 0.45, y: 0
Bytes: 5.15E+08 -- x: 0.5, y: 0
Bytes: 5.2E+08 -- x: 0.5, y: 0
Bytes: 5.25E+08 -- x: 0.5, y: 0
Bytes: 5.3E+08 -- x: 0.5, y: 0
Bytes: 5.35E+08 -- x: 0.5, y: 0
Bytes: 5.4E+08 -- x: 0.5, y: 1
Bytes: 5.45E+08 -- x: 0.5, y: 1
Bytes: 5.5E+08 -- x: 0.5, y: 1
Bytes: 5.55E+08 -- x: 0.5, y: 1
Bytes: 5.6E+08 -- x: 0.5, y: 1
Bytes: 5.65E+08 -- x: 0.55, y: 1
Bytes: 5.7E+08 -- x: 0.55, y: 1
Bytes: 5.75E+08 -- x: 0.55, y: 1
Bytes: 5.8E+08 -- x: 0.55, y: 1
Bytes: 5.85E+08 -- x: 0.55, y: 1
Bytes: 5.9E+08 -- x: 0.55, y: 1
Bytes: 5.95E+08 -- x: 0.55, y: 1
Bytes: 6E+08 -- x: 0.55, y: 1
Bytes: 6.05E+08 -- x: 0.55, y: 1
Bytes: 6.1E+08 -- x: 0.55, y: 1
Bytes: 6.15E+08 -- x: 0.55, y: 1
Bytes: 6.2E+08 -- x: 0.6, y: 1
Bytes: 6.25E+08 -- x: 0.6, y: 1
Bytes: 6.3E+08 -- x: 0.6, y: 1
Bytes: 6.35E+08 -- x: 0.6, y: 1
Bytes: 6.4E+08 -- x: 0.6, y: 1
Bytes: 6.45E+08 -- x: 0.6, y: 1
Bytes: 6.5E+08 -- x: 0.6, y: 1
Bytes: 6.55E+08 -- x: 0.6, y: 1
Bytes: 6.6E+08 -- x: 0.6, y: 1
Bytes: 6.65E+08 -- x: 0.6, y: 1
Bytes: 6.7E+08 -- x: 0.6, y: 1
Bytes: 6.75E+08 -- x: 0.65, y: 1
Bytes: 6.8E+08 -- x: 0.65, y: 1
Bytes: 6.85E+08 -- x: 0.65, y: 1
Bytes: 6.9E+08 -- x: 0.65, y: 1
Bytes: 6.95E+08 -- x: 0.65, y: 1
Bytes: 7E+08 -- x: 0.65, y: 1
Bytes: 7.05E+08 -- x: 0.65, y: 1
Bytes: 7.1E+08 -- x: 0.65, y: 1
Bytes: 7.15E+08 -- x: 0.65, y: 1
Bytes: 7.2E+08 -- x: 0.65, y: 1
Bytes: 7.25E+08 -- x: 0.7, y: 1
Bytes: 7.3E+08 -- x: 0.7, y: 1
Bytes: 7.35E+08 -- x: 0.7, y: 1
Bytes: 7.4E+08 -- x: 0.7, y: 1
Bytes: 7.45E+08 -- x: 0.7, y: 1
Bytes: 7.5E+08 -- x: 0.7, y: 1
Bytes: 7.55E+08 -- x: 0.7, y: 1
Bytes: 7.6E+08 -- x: 0.7, y: 1
Bytes: 7.65E+08 -- x: 0.7, y: 1
Bytes: 7.7E+08 -- x: 0.7, y: 1
Bytes: 7.75E+08 -- x: 0.7, y: 1
Bytes: 7.8E+08 -- x: 0.75, y: 1
Bytes: 7.85E+08 -- x: 0.75, y: 1
Bytes: 7.9E+08 -- x: 0.75, y: 1
Bytes: 7.95E+08 -- x: 0.75, y: 1
Bytes: 8E+08 -- x: 0.75, y: 1
Bytes: 8.05E+08 -- x: 0.75, y: 1
Bytes: 8.1E+08 -- x: 0.75, y: 1
Bytes: 8.15E+08 -- x: 0.75, y: 1
Bytes: 8.2E+08 -- x: 0.75, y: 1
Bytes: 8.25E+08 -- x: 0.75, y: 1
Bytes: 8.3E+08 -- x: 0.75, y: 1
Bytes: 8.35E+08 -- x: 0.8, y: 1
Bytes: 8.4E+08 -- x: 0.8, y: 1
Bytes: 8.45E+08 -- x: 0.8, y: 1
Bytes: 8.5E+08 -- x: 0.8, y: 1
Bytes: 8.55E+08 -- x: 0.8, y: 1
Bytes: 8.6E+08 -- x: 0.8, y: 1
Bytes: 8.65E+08 -- x: 0.8, y: 1
Bytes: 8.7E+08 -- x: 0.8, y: 1
Bytes: 8.75E+08 -- x: 0.8, y: 1
Bytes: 8.8E+08 -- x: 0.8, y: 1
Bytes: 8.85E+08 -- x: 0.8, y: 1
Bytes: 8.9E+08 -- x: 0.85, y: 1
Bytes: 8.95E+08 -- x: 0.85, y: 1
Bytes: 9E+08 -- x: 0.85, y: 1
Bytes: 9.05E+08 -- x: 0.85, y: 1
Bytes: 9.1E+08 -- x: 0.85, y: 1
Bytes: 9.15E+08 -- x: 0.85, y: 1
Bytes: 9.2E+08 -- x: 0.85, y: 1
Bytes: 9.25E+08 -- x: 0.85, y: 1
Bytes: 9.3E+08 -- x: 0.85, y: 1
Bytes: 9.35E+08 -- x: 0.85, y: 1
Bytes: 9.4E+08 -- x: 0.9, y: 1
Bytes: 9.45E+08 -- x: 0.9, y: 1
Bytes: 9.5E+08 -- x: 0.9, y: 1
Bytes: 9.55E+08 -- x: 0.9, y: 1
Bytes: 9.6E+08 -- x: 0.9, y: 1
Bytes: 9.65E+08 -- x: 0.9, y: 1
Bytes: 9.7E+08 -- x: 0.9, y: 1
Bytes: 9.75E+08 -- x: 0.9, y: 1
Bytes: 9.8E+08 -- x: 0.9, y: 1
Bytes: 9.85E+08 -- x: 0.9, y: 1
Bytes: 9.9E+08 -- x: 0.9, y: 1
Bytes: 9.95E+08 -- x: 0.95, y: 1
like image 193
Sean Sailer Avatar answered Sep 27 '22 20:09

Sean Sailer