简体   繁体   中英

dividing efficiently

I want to divide a number of type double, by an int. I only need the result in 2 deicmal place in string format. What is the best way to do this in terms of stack efficiency?

double d=321321321313131233213213213213;
int i=123;

ToString(d/i); //what do I get when I do this? A double? A float?

       public String ToString(float? result) //what should I cast the result?
        {
            return @String.Format("{0:#,0.##;} divided double by int", result);
        }

ToString(d/i); //what do I get when I do this? A double? A float?

You'll get a double. Dividing a double by an int will result in a double value.

public String ToString(float? result) //what should I cast the result?

You'll get a double for the value, so you'd be better off just using a double here. You'll never get a nullable type, and definitely wouldn't get a float as a result, so using float? is wholly inappropriate.

What is the best way to do this in terms of stack efficiency?

This is really not worth worrying about, unless, of course, you profile and find this really happens to be a problem. Building the string will be far more expensive than the division operation, and neither is likely to be a hotspot in terms of performance.


A clean way to handle this would just be to use double.ToString("N2") , ie:

double result = d/i;
string resultAsString = result.ToString("N2");

If you want a full, formatted string, you can use:

string resultAsString = string.Format("{0:N2} divided double by int", result);

Ok. First off, you get double , not double? or float? . Math functions should never return a nullable type. If you are assuming that infinity is represented as null , you are wrong. Infinities are determined by the bits. You can see if a double is infinity with Double.IsInfinity(double) .

[SecuritySafeCritical]
public static unsafe bool IsInfinity(double d)
{
    return ((*(((long*) &d)) & 0x7fffffffffffffffL) == 0x7ff0000000000000L);
}

What is the best way to do this in terms of stack efficiency?

I would recommend a look at the tragedies of micro optimization over at Coding Horror.


As for the function. That is completely unnecessary. Your best bet is Double.ToString(...) :

double val = d / i;
string result = val.ToString("N2");

However, if you are using String.Format(...) , you can use:

double val = d / i;
string result = string.Format("{0:N2} divided double by int", val);

Hacky solution:

If you need only 2 decimal places, you can multiply your double by 100, convert it to an integer, and then divide it by i .

Now take the string of the result, and add a dot before the last two chars.

Don't forget to handle the edge cases of negative values and rounding problems.

If you really care about efficiency, division of fractions is a slow process in comparison to division of integers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM