Giter Site home page Giter Site logo

Comments (6)

mvandervoord avatar mvandervoord commented on July 24, 2024

Ha!

This is most definitely a bug. I'll look into cleaning this function up, unless you have a suggestion already. I'm not a fan of using a larger type, because we can't guarantee that all targets will have a larger type available. I'm sure we can fix it somehow, though.

Mark

from unity.

SigmaPic avatar SigmaPic commented on July 24, 2024

Indeed, I have a better solution than using a 64-bit data type.
Just declaring the variable "number" as a _U_UINT instead of a _U_SINT and changing a litlle bit the sign check should do the trick.

This is my suggestion:

void UnityPrintNumber(const _U_SINT number_to_print)
{
    _U_SINT divisor = 1;
    _U_SINT next_divisor;
    _U_UINT number;

    if (number_to_print < 0)
    {
        UNITY_OUTPUT_CHAR('-');
        number = -number_to_print;
    }
    else
    {
        number = number_to_print;
    }

    // figure out initial divisor
    while (number / divisor > 9)
    {
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
    }

    // now mod and print, then divide divisor
    do
    {
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
        divisor /= 10;
    }
    while (divisor > 0);
}

from unity.

mvandervoord avatar mvandervoord commented on July 24, 2024

Awesome. This is a good solution. I've committed that change (e2d5e1c). I think this fixes this issue. Thanks for bringing it up (and for fixing it!)

from unity.

mjago avatar mjago commented on July 24, 2024

You're saying number is _U_UINT (unsigned) and yet you propose number = -number_to_print;
Wrong for many reasons across many compilers no?
Best,
Martyn

On 1 Sep 2014, at 21:38, SigmaPic [email protected] wrote:

_U_UINT number;

if (number_to_print < 0)
{
    UNITY_OUTPUT_CHAR('-');
    number = -number_to_print;
}

from unity.

SigmaPic avatar SigmaPic commented on July 24, 2024

I agree that it's a little bit doubtful. However, I think that is kind of basic arithmetic is the same for all compilers.
I have only tested with MinGW. To be sure, we have to check in the ANSI C standard.

Or maybe something like that, but you have to define _U_SINT_MIN in order to be compatible to 32 and 64-bit mode.

I don't no, it's only suggestions. I let you choose the best solution.

void UnityPrintNumber(const _U_SINT number_to_print)
{
    _U_SINT divisor = 1;
    _U_SINT next_divisor;
    _U_UINT number;

    if (number_to_print == _U_SINT_MIN)
    {
        UNITY_OUTPUT_CHAR('-');
        number = (_U_UINT)number_to_print;
    }
    else if (number < 0)
    {
        UNITY_OUTPUT_CHAR('-');
        number = -number_to_print;
    }
    else
    {
        number = number_to_print;
    }

    // figure out initial divisor
    while (number / divisor > 9)
    {
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
    }

    // now mod and print, then divide divisor
    do
    {
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
        divisor /= 10;
    }
    while (divisor > 0);
}

from unity.

mvandervoord avatar mvandervoord commented on July 24, 2024

Martyn: Good though. I'm glad you double checked this.

The ANSI C standard has the negative unary operator on the same "level" as casting for its order of operations. I think this means that the negation should happen first, which the negative of 0x80000000 is undefined according to C99 ยง7.20.6.1/2. Undefined is definitely NOT what we are looking for. ;)

Therefore, I'm updating to SigmaPic's second solution. This one appears like it should work universally. I've tweaked it slightly to explicitly cast. If you have a moment, I'd love it if you would double check it. (f480051)

Thanks again, both of you.

from unity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.