Viewing 36 posts - 1 through 36 (of 36 total)
  • C# Maths function (geek programming content)
  • jimmers
    Free Member

    In simple terms I want to find out the order of magnitude for a given number.

    e.g.

    1,000 = 3
    100,000 = 5

    On Windows 7 the built-in calculator (in scientific mode) has a log function so log(1000) = 3.

    Does anyone know the equivalent function in C#. Brain fried trying to remember A level Maths…

    Torminalis
    Free Member

    Math.Ceiling(Math.Log10(x))

    Torminalis
    Free Member

    no prize?

    fisha
    Free Member

    here you go …

    // Prize.cs
    public class stwPrize
    {
    public static void Main()
    {
    System.Console.WriteLine(“Torminalis, You Win!”);
    }
    }

    Torminalis
    Free Member

    😀

    jimmers
    Free Member

    Cheers Torm, that did the trick perfectly you win first prize!

    roady_tony
    Free Member

    you guys and your modern programming….

    i remember writing that in pure C once
    and also in …………………………………
    ASSEMBLER!

    GrahamS
    Full Member

    In future try http://www.stackoverflow.com – more geeks than you can shake a sweaty Megadeth t-shirt at.

    simonfbarnes
    Free Member

    uh, it doesn’t work for negative numbers, and shouldn’t it be Floor() ?

    TheBrick
    Free Member

    It won’t as log of anything less than 0 is complex and fortran is the only language (I know of) that natively supports complex numbers.

    Take the absolute value first, just about every language had intrinsic funciotn for htese basisc maths such abs, log, mod e.t.c

    poly
    Free Member

    uh, it doesn’t work for negative numbers, and shouldn’t it be Floor() ?

    you can’t compute the log of a (real) negative number…

    EDIT:

    TheBrick – Member

    It won’t as log of anything less than 0 is complex and fortran is the only language (I know of) that natively supports complex numbers.

    Take the absolute value first, just about every language had intrinsic funciotn for htese basisc maths such abs, log, mod e.t.c
    Ah too slow! – Python natively supports complex numbers, but even then it wouldn’t automatically return a complex result from the log(x) function.

    TheBrick
    Free Member
    simonfbarnes
    Free Member

    you can’t compute the log of a (real) negative number…

    I know, that why it doesn’t work for them…
    and doesn’t Ceiling() round up ?

    CharlieMungus
    Free Member

    if torm’s perfect answer isn’t good enough you convert it to a string then interrogate the length of it.

    TheBrick
    Free Member

    Ah too slow! – Python natively supports complex numbers, but even
    then it wouldn’t automatically return a complex result from the log(x) function.

    Interesting. I wrote my first python script a few weeks ago to do some simple text parsing and thought it was quite a cool little language (in my limited experience). I think it’s even better now.

    simonfbarnes
    Free Member

    if torm’s perfect answer isn’t good enough you convert it to a string then interrogate the length of it.

    unless negative or containing decimals…

    CharlieMungus
    Free Member

    unless negative or containing decimals…

    oh true, there’s no way of overcoming those issues

    simonfbarnes
    Free Member

    oh true, there’s no way of overcoming those issues

    I didn’t mean that – but programming has to be exact, not just winging it and hoping for the best 🙂

    Torminalis
    Free Member

    but programming has to be exact, not just winging it and hoping for the best

    There was no specification of needing to deal with anything but unsigned integers, if you look at the OP he did in fact specify that he was looking for the equivalent of an existing function that he had already identified as doing the job and I merely pointed him to the equivalent function in C#.

    I take it you work in research and not in the real world? 😉

    roady_tony
    Free Member

    i seem to remember a classic Picture of a Swing, and its stages through the development life cycle.

    ahhh…..how things DONT change!

    simonfbarnes
    Free Member

    There was no specification of needing to deal with anything but unsigned integers

    neither was there anything to exclude them. 2 examples is hardly a specifiucation. Surely the “real world” does throw up exactly this kind of unexpected eventuality ?

    warton
    Free Member

    aaah, in two weeks I leave programming to start a role in functional design / system reqs.
    I am so happy, I’ve always struggled with programming, been doing it professionally for 3 years now, yes I can write code that works, but I’ve never really enjoyed it…

    jimmers
    Free Member

    Anyhow for the avoidance of any doubt, Torm’s code is now embedded into the codebase of a future production release.

    I only need to deal with postive values so it does what it says on the tin. I think I’ve spent too much time coding PHP and forgotten what C# looks like!

    toby1
    Full Member

    FFS the original poster is happy with the solution why do you need to try and pick holes in it. If it’s fit for purpose then it’s fine and I’m sure the OP will be validating the input before attempting to find the log of a negative, ok now?

    simonfbarnes
    Free Member

    If it’s fit for purpose then it’s fine

    “if” being the appropriate word, and since when was the user responsible for robust program design ? Remember 2 digits for the year ?

    Torminalis
    Free Member

    “if” being the appropriate word, and since when was the user responsible for robust program design ? Remember 2 digits for the year ?

    Do you really not have anything better to do?

    simonfbarnes
    Free Member

    and don’t forget zero is a valid positive number yet its order of magnitude is embarrassingly -infinity…

    Do you really not have anything better to do?

    tell that to the millenium

    Torminalis
    Free Member

    If you have a better solution, show us.

    simonfbarnes
    Free Member

    If you have a better solution, show us.

    do we know what the question is yet ?

    CharlieMungus
    Free Member

    Zero is great! It does all kind of freaky things.

    CharlieMungus
    Free Member

    do we know what the question is yet ?

    On Windows 7 the built-in calculator (in scientific mode) has a log function so log(1000) = 3.

    Does anyone know the equivalent function in C#.

    jimmers
    Free Member

    If you want to know the gory details it is to calculate the order of magnitude of a cost (so this will always be positive as in business there is no such thing as a negative cost and the system does not allow negative values via the data entry validation).

    The plan is to figure out the order of magnitude of the maximum value of a data point plotted on the axis of a bar chart. And this needs to be a round figures to look nice. eg

    max x value = 965 -> x-axis range = 1000
    max x value = 6713 -> x-axis range = 6800

    i.e. 965.

    Math.Ceiling(Math.log10(965)) = 3

    Minus 1 from [order of magnitude] (if greater than 1)

    This would be 2.

    Back again

    Math.Pow(10, 2) = 100.

    Calculate the max figure to the nearest 100 = 900

    Add the interval to this figure = 900 + 100

    Max x-axis value = 1000

    Voila

    But Crystal Report 2008 does not allow one to programmatically set the x-axis max range (you can only do this via the designer).

    So its all been for nowt, ah well that’s dev for you. Back to my cup of coffee and the next bug…

    CharlieMungus
    Free Member

    I wrote something very similar in Flash Action script. for a plotter. The max axis calculation leads to some strange axis values, which can lead to strange intervals, if you fix the number of intervals. Folks are much happier reading from charts with familiar maxima, e.g 100,200,500,1000 etc. Anything else tends to lead to misinterpretation of the data.

    toby1
    Full Member

    Check it out the OP knew the question after all, so no need for anyone else to ask if for him 🙂

    Ah Crystal reports – best when in the dustbin!

    jimmers
    Free Member

    Crystal Reports PITAS…

    If you set the report to auto-range on a given axis and the report is say 5 pages long. The x-axis max range will be set to the max value for that page. So the max range for page 1 may be 8000, page 2 is 200, etc.

    My idea was to set this programmatically and disable the auto-range property. My mistake was to assume that CR2008 supports setting the max-range value dynamically.

    simonfbarnes
    Free Member

    as in business there is no such thing as a negative cost

    YAY! Trebles and bonusses all round ?

Viewing 36 posts - 1 through 36 (of 36 total)

The topic ‘C# Maths function (geek programming content)’ is closed to new replies.