ChronoDK / GodotBigNumberClass

Use very BIG numbers in Godot Engine games.
MIT License
116 stars 27 forks source link

Logarithmic notation #5

Closed Drillur closed 1 year ago

Drillur commented 4 years ago

Hey, I don't know how to make a code submission to your project, so I'm just writing it here.

I wrote logarithmic notation for your Big class. You don't have to implement it, just thought I'd share what I added for my game.

Some examples of going from scientific to log notation:

1e10 = e10 2e10 = e10.3 6.5e10 = e10.81 9e10 = e10.95

The code is below. It only works with positive numbers.

-

Thanks for like the 3rd time for making BigNumberClass!

func toLog():

    var dec = str(stepify(abs(log(mantissa) / log(10) * 10), 0.01))
    dec = dec.replace(".", "")

    if dec == "10":
        dec = "999"

    if dec != "0":
        dec = "." + dec
    else:
        dec = ""

    return "e" + format_exponent(exponent) + dec

func format_exponent(value) -> String:

        if value < 1000:
        return str(value) # 100

        # above: for numbers < 1000
    # below: for numbers >= 1,000 and < 1,000,000

    var string = str(value)
    var mod = string.length() % 3
    var output = ""

    for i in range(0, string.length()):
        if i != 0 && i % 3 == mod:
            output += ","
        output += string[i]

    return output # 342,945
ChronoDK commented 4 years ago

That's cool! Love to see people using and expanding the code. I have no idea what logarithmic notation is, and why it's useful - can you explain how and why you use it in your game?

Drillur commented 4 years ago

Log notation is like scientific notation, just a way to display a huge number, but it is a little different!

First, it displays the exponent first. For 1 million, it would read e6 which is 1e6 in scientific.

Next, it still shows the mantissa, but as a decimal after the exponent. For 5 million, which is 5e6 in scientific, it would read e6.7 in log notation. It's .7, instead of .5, because the mantissa is calculated in log base 10.

In effect, when an in-game resource like gold is going up incrementally, in log notation it appears to go up quickly and then slows down at the end of the exponent. This keeps things very relative. At the beginning of the next power (e7), the gold count continues to appear to be slowing down in speed, even if though it is increasing incrementally.

To be clear, I don't have a full understanding of log in the real world of math, but I have been using log notation while I test it, and I have enjoyed seeing the exponent in the forefront of the number.

I added this because I have added options for notation that the players may choose, and logarithmic notation is the up-and-coming choice for seasoned idle game players.

-

In the code I've posted above, it will at most display 3 decimals after the exponent, with as little as no decimals (like e10).

In the format_exponent function, that is just in case the exponent ever goes above 1000, which is unlikely in most games, but I thought I'd add it just in case. (In the case of 500k exponent, it would return "500,000" .)

ChronoDK commented 1 year ago

I have added this now. Adjusted your code to support some of the other features in the class. Better late than never right ;-)

ChronoDK commented 1 year ago

Function is called toLogarithmic()