What is log n? menu

User Tag List

Results 1 to 14 of 14
  1. #1
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is log n?

    Code:
    if (Value == 0) return "0";
    if (Value < 100L * 1000L * 1000 * 1000 * 1000)
    {
        if (Value < 1000L * 1000 * 1000 * 1000)
        {
            if (Value < 100L * 1000 * 1000 * 1000)
            {
                if (Value < 1000 * 1000 * 1000)
                {
                    if (Value < 100 * 1000 * 1000)
                    {
                        if (Value < 1000 * 1000)
                        {
                            if (Value < 10 * 1000)
                            {
                                if (Value < 1 * 1000)
                                {
                                    if (Value < 1 * 100)
                                    {
                                        return Value.ToString("0.0", CultureInfo.InvariantCulture);
                                    }
                                    else return Value.ToString("0", CultureInfo.InvariantCulture);
                                }
                                else return Value.ToString("#,0", CultureInfo.InvariantCulture);
                            }
                            else return (Value / 1000.0f).ToString("#,0k", CultureInfo.InvariantCulture);
                        }
                        else return (Value / 1000.0f / 1000.0f).ToString("#,0.0M", CultureInfo.InvariantCulture);
                    }
                    else return (Value / 1000.0f / 1000.0f).ToString("#,0M", CultureInfo.InvariantCulture);
                }
                else return (Value / 1000.0f / 1000.0f / 1000.0f).ToString("#,0.0bn", CultureInfo.InvariantCulture);
            }
            else return (Value / 1000.0f / 1000.0f / 1000.0f).ToString("#,0bn", CultureInfo.InvariantCulture);
        }
        else return (Value / 1000.0f / 1000.0f / 1000.0f / 1000.0f).ToString("#,0.0tr", CultureInfo.InvariantCulture);
    }
    else return (Value / 1000.0f / 1000.0f / 1000.0f / 1000.0f).ToString("#,0tr", CultureInfo.InvariantCulture);
    Code:
               
    if (Value == 0) return "0"; //0
    var v = (int)Math.Log10(Value);
    switch (v) {
        case 0:
        case 1:
            return Value.ToString("0.0", CultureInfo.InvariantCulture);  // 1 - 99
        case 2:
            return Value.ToString("0", CultureInfo.InvariantCulture); // 100 - 999
        case 3:
            return Value.ToString("#,0", CultureInfo.InvariantCulture); // 1,000 - 9,999
        case 4:
        case 5:
            return (Value / 1000F).ToString("#,0k", CultureInfo.InvariantCulture); // 10,000 - 999,999
        case 6:
        case 7:
            return (Value / 1000000F).ToString("#,0.0M", CultureInfo.InvariantCulture); // 1,000,000 - 99,999,999
        case 8:
            return (Value / 1000000F).ToString("#,0M", CultureInfo.InvariantCulture); // 100,000,000 - 999,999,999
        case 9:
        case 10:
            return (Value / 1000000000F).ToString("#,0.0bn", CultureInfo.InvariantCulture); // 1,000,000,000 - 99,999,999,999
        case 11:
            return (Value / 1000000000F).ToString("#,0bn", CultureInfo.InvariantCulture); // 100,000,000,000 - 999,999,999,999
        case 12:
        case 13:
            return (Value / 1000000000000F).ToString("#,0.0tr", CultureInfo.InvariantCulture); // 1,000,000,000,000 - 99,999,999,999,999
        default:
            return (Value / 1000000000000F).ToString("#,0tr", CultureInfo.InvariantCulture); // 100,000,000,000,000+
    }
    My aim here is to perhaps provide code that is more readable? Also doing divides like that actually causes the execution to force an individual divide each time.

    What is log n?
  2. Thanks bobindeed (1 members gave Thanks to g1xb17 for this useful post)
  3. #2
    Neherson's Avatar Member
    Reputation
    3
    Join Date
    Jul 2017
    Posts
    1
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    c# - String Format Numbers Thousands 123K, Millions 123M, Billions 123B - Stack Overflow

    "#,##0,,,B" would format in billions.

    Also, the division in your second version only happens if the corresponding branch gets executed, so don't worry about it. With trailing commas in the format string it's cleaner too, even if you were to rewrite the first version by turning those nested ifs inside-out (as you should have). You could use an array of format strings instead of a case (naturally with a default string if the order of magnitude is out of bounds). The code will get shorter, but then you'll have data, it's not obviously better for such a small piece.

    Also, it's not obvious why you need such a complicated formatting in the first place. It's a bit of a mess, that's why the code (or data) gets messy.

  4. Thanks JackCeparou, bobindeed (2 members gave Thanks to Neherson for this useful post)
  5. #3
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Neherson View Post
    c# - String Format Numbers Thousands 123K, Millions 123M, Billions 123B - Stack Overflow

    "#,##0,,,B" would format in billions.

    Also, the division in your second version only happens if the corresponding branch gets executed, so don't worry about it. With trailing commas in the format string it's cleaner too, even if you were to rewrite the first version by turning those nested ifs inside-out (as you should have). You could use an array of format strings instead of a case (naturally with a default string if the order of magnitude is out of bounds). The code will get shorter, but then you'll have data, it's not obviously better for such a small piece.

    Also, it's not obvious why you need such a complicated formatting in the first place. It's a bit of a mess, that's why the code (or data) gets messy.
    Or...

    Code:
    return Value < 100L * 1000L * 1000 * 1000 * 1000 ? Value < 1000L * 1000 * 1000 * 1000 ? Value < 100L * 1000 * 1000 * 1000 ? Value < 1000 * 1000 * 1000 ? Value < 100 * 1000 * 1000 ? Value < 1000 * 1000 ? Value < 10 * 1000 ? Value < 1 * 1000 ? Value < 1 * 100 ? Value == 0 ? "0" : Value.ToString("0.0", CultureInfo.InvariantCulture) : Value.ToString("0", CultureInfo.InvariantCulture) : Value.ToString("#,0", CultureInfo.InvariantCulture) : (Value / 1000.0f).ToString("#,0k", CultureInfo.InvariantCulture) : (Value / 1000.0f / 1000.0f).ToString("#,0.0M", CultureInfo.InvariantCulture) :
                    (Value / 1000.0f / 1000.0f).ToString("#,0M", CultureInfo.InvariantCulture) : (Value / 1000.0f / 1000.0f / 1000.0f).ToString("#,0.0bn", CultureInfo.InvariantCulture) : (Value / 1000.0f / 1000.0f / 1000.0f).ToString("#,0bn", CultureInfo.InvariantCulture) :
                    (Value / 1000.0f / 1000.0f / 1000.0f / 1000.0f).ToString("#,0.0tr", CultureInfo.InvariantCulture) : (Value / 1000.0f / 1000.0f / 1000.0f / 1000.0f).ToString("#,0tr", CultureInfo.InvariantCulture);
    The very pinacle of saving space in code. I reject your fucking if statements and substitute my own cancer.

  6. #4
    JackCeparou's Avatar Savvy ? 🐒
    Reputation
    534
    Join Date
    Mar 2017
    Posts
    588
    Thanks G/R
    51/490
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by g1xb17 View Post
    Also doing divides like that actually causes the execution to force an individual divide each time.
    You know than the compiler optimise this, right ?

    PHP Code:
    void Main()
    {
        
    Console.WriteLine(GetA());
        
    Console.WriteLine(GetB());
    }

    int GetA() 
    {
        
    int x 1000000 1000;
        return 
    x;
    }
    int GetB() 
    {
        
    int x 1000;
        return 
    x;

    Corresponding IL :
    PHP Code:
    IL_0000:  nop         
    IL_0001
    :  nop         
    IL_0002
    :  nop         
    IL_0003
    :  nop         
    IL_0004
    :  ret         

    g__Main0_0
    :
    IL_0000:  nop         
    IL_0001
    :  call        g__GetA0_1
    IL_0006
    :  call        System.Console.WriteLine
    IL_000B
    :  nop         
    IL_000C
    :  call        g__GetB0_2
    IL_0011
    :  call        System.Console.WriteLine
    IL_0016
    :  nop         
    IL_0017
    :  ret         

    g__GetA0_1
    :
    IL_0000:  nop         
    IL_0001
    :  ldc.i4      E8 03 00 00 
    IL_0006
    :  stloc.0     // x
    IL_0007:  ldloc.0     // x
    IL_0008:  stloc.1     
    IL_0009
    :  br.s        IL_000B
    IL_000B
    :  ldloc.1     
    IL_000C
    :  ret         

    g__GetB0_2
    :
    IL_0000:  nop         
    IL_0001
    :  ldc.i4      E8 03 00 00 
    IL_0006
    :  stloc.0     // x
    IL_0007:  ldloc.0     // x
    IL_0008:  stloc.1     
    IL_0009
    :  br.s        IL_000B
    IL_000B
    :  ldloc.1     
    IL_000C
    :  ret 
    Look at 'IL_0001: ldc.i4 E8 03 00 00', it's the same in both methods. aka x3E8 == 1000 in decimal.

    /popcorn
    Hide the Rum! --> Default theme customization 101 <--

  7. #5
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I tried to copy paste some code in but, it appears that the site does not like the reply I'm trying to post.

    However, I encourage you to try the same test but without using straight up constants, try with a variable such that its like Value / 1000 / 1000;

    The C# compiled ILASM is not simplifying it for me. It does however simplify 1000000/1000. But, it's not the same as Value / 1000 / 1000;

    It doesn't work the same.

    Again my point was not so much this, it was that it wasn't nearly as readable. And the ternary was just me joking with the guy who posted at me about the ifs.

    Edit:

    Thats the code for Value / 1000 / 1000
    Code:
    	IL_0000: ldarg.0
    	IL_0001: ldc.i4 1000
    	IL_0006: conv.i8
    	IL_0007: div
    	IL_0008: ldc.i4 1000
    	IL_000d: conv.i8
    	IL_000e: div
    	IL_000f: conv.r8
    	IL_0010: ret
    This is what you're trying to prove that 1000000/1000 does. The compiler does simplify constants it can simplify easily as far as I already knew.
    Except in the previous case.
    Code:
    	IL_0000: ldc.r8 1000
    	IL_0009: ret
    Last edited by g1xb17; 08-08-2017 at 05:44 AM.

  8. #6
    JackCeparou's Avatar Savvy ? 🐒
    Reputation
    534
    Join Date
    Mar 2017
    Posts
    588
    Thanks G/R
    51/490
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, i should have shut up and do my work instead ^^
    This need more research, but interesting point.

    About readability, yeah sure, but who cares here ?
    It's : Write once, never touch it again.

    Ps: I keep eating popcorn anyway ;p
    Hide the Rum! --> Default theme customization 101 <--

  9. #7
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by JackCeparou View Post
    True, i should have shut up and do my work instead ^^
    This need more research, but interesting point.

    About readability, yeah sure, but who cares here ?
    It's : Write once, never touch it again.

    Ps: I keep eating popcorn anyway ;p
    I guess I care lol. The readability is what makes it useable. Sometimes understanding what something does (AKA reading the written implementation) makes it easier to use. Instead of guesswork. Especially when you have a zero API setup such as this. Some of the code is hard-ish to follow. But, I'd give this codebase an 8/10 compared to most of the codebases I've ever seen. So when you have zero api doc sorta setup, the readability of the code counts. The only other thing I might suggest as an outsider looking in, is to put some shitty comments on some of the more obscure interfaces. That's all I'd like, then it'd be 10/10. I'd even do it myself if I was part of the project and knew more about the tool and what it's doing. Maybe it's because I'm used to C#, but this really is one of the easiest apis I've ever used.

    I have been writing my own plugin and it's been pretty easy but, as with anything, learning how the codebase is used is the real challenge and thats experience at that point.

  10. #8
    JackCeparou's Avatar Savvy ? 🐒
    Reputation
    534
    Join Date
    Mar 2017
    Posts
    588
    Thanks G/R
    51/490
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, if find it more clear without comments than many 'professional' paid api/libs/tools with (outdated) docs..

    Well, sorry for your ocd if you click this link ;p
    Hide the Rum! --> Default theme customization 101 <--

  11. #9
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by JackCeparou View Post
    Yeah, if find it more clear without comments than many 'professional' paid api/libs/tools with (outdated) docs..

    Well, sorry for your ocd if you click this link ;p

    Hah, Well that's actually pretty terrible and a level beyond what I am crying about.

    Also, I don't think this codebase necessarily needs much comments to be good. There are even some comments in there. You don't need a professional style XML doc or whatever the fuck.

    Some stuff is not obvious and thats what I refer to when i say a few shitty comments.

    But, it's really not a big deal, between other plugins and what I now realize that Default has a lot of this stuff, It really isn't hard for an idiot to figure out. I wouldn't mind commenting stuff I figure out in my version and someone can git compare and save them in.

    EDIT: I just see a lot of request for that and I think if the community wants comments, they'll have to contribute that in themselves maybe? I dunno if I'm out of line for even offering or thinking It's something that could be done by the community, but, if I speak for myself, I wouldn't mind at all.
    Last edited by g1xb17; 08-08-2017 at 08:49 AM.

  12. #10
    JackCeparou's Avatar Savvy ? 🐒
    Reputation
    534
    Join Date
    Mar 2017
    Posts
    588
    Thanks G/R
    51/490
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slightly off-topic, but anyway.

    The possibility to host all default / interfaces inside a github repo was already discussed with KJ, to be precise we were discuting it when the old board was shutdown ^^'

    This open way more possibilities than just comments, i would love to be able to make some PR there too ;p
    (and this will avoid some random dude to paste everything himself as i have seen recently)

    Btw, i don't see any value to comment /interfaces/ really, i'm pretty sure than the majority reclaiming comments/docs don't even use VS for editing.
    Note than i'm maybe biased because i swim in it since months now..
    Hide the Rum! --> Default theme customization 101 <--

  13. #11
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by JackCeparou View Post
    Slightly off-topic, but anyway.

    The possibility to host all default / interfaces inside a github repo was already discussed with KJ, to be precise we were discuting it when the old board was shutdown ^^'

    This open way more possibilities than just comments, i would love to be able to make some PR there too ;p
    (and this will avoid some random dude to paste everything himself as i have seen recently)

    Btw, i don't see any value to comment /interfaces/ really, i'm pretty sure than the majority reclaiming comments/docs don't even use VS for editing.
    Note than i'm maybe biased because i swim in it since months now..
    I think trying to develop anything in a codebase this large or larger without an IDE is basically like shooting yourself in the foot for no reasons other than to spite yourself. I know there's like a 50/50 split or something in terms of what people prefer, No IDE, gotta have an IDE. There's nothing wrong with not using one, but, for the purposes of basically having to open several files to be sure you're writing the correct code or to check something is a great time waster. That and early detection of errors. There's really more reasons to use an IDE than not use one. But, If you were just writing some script, it wouldn't matter either way. Somehow people who are hardcore think this really matters in proving how good you are as a programmer and look down on people who do use them. Being efficient counts as well. So not using every available advantage is basically like saying "I am too stupid to use tools that would increase my productivity and efficiency". But also claiming that others are dumb for using them because it makes them worse? Thats like also saying "Hey, You should listen to me and do what I do and also exclude yourself from anything that might actually be useful to you because arbitrary personal feelings matter when it comes to productivity"

    At work almost everyone uses IDEs, and one guy was showing me some code and he was like fumbling around in the console trying to open up shit in VIM. Catch me using VIM with thousands of files.

  14. #12
    JackCeparou's Avatar Savvy ? 🐒
    Reputation
    534
    Join Date
    Mar 2017
    Posts
    588
    Thanks G/R
    51/490
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by g1xb17 View Post
    Catch me using VIM with thousands of files.
    Every damn time...
    Trapped | CommitStrip
    Hide the Rum! --> Default theme customization 101 <--

  15. #13
    g1xb17's Avatar Member
    Reputation
    2
    Join Date
    Aug 2017
    Posts
    11
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel like thats pretty accurate. I've had VIM lock up on me so hard I have had to reboot. There's no way to get out. Even worse if you're on a headless machine shelled in. Gotta backout and detach the screen and yep, catch me using VIM in general. Rather drink bleach.

  16. #14
    prrovoss's Avatar Contributor
    Reputation
    152
    Join Date
    Jan 2013
    Posts
    420
    Thanks G/R
    23/130
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  17. Thanks JackCeparou, SeaDragon (2 members gave Thanks to prrovoss for this useful post)

Similar Threads

  1. What is in Blizz logs?
    By Arcane36 in forum World of Warcraft General
    Replies: 2
    Last Post: 07-04-2012, 09:46 AM
  2. exactly what is model editing
    By Zanatons in forum World of Warcraft General
    Replies: 10
    Last Post: 12-19-2006, 02:00 PM
  3. What is the warden, and what doe it do?
    By WoWLegend in forum World of Warcraft General
    Replies: 2
    Last Post: 09-24-2006, 07:04 PM
  4. What is your favorite bot for WoW?
    By koalaz2004 in forum World of Warcraft General
    Replies: 2
    Last Post: 08-17-2006, 10:28 PM
  5. ALSO what or what is syndrom really usefull for?
    By case in forum World of Warcraft General
    Replies: 2
    Last Post: 03-20-2006, 11:00 PM
All times are GMT -5. The time now is 11:21 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search