Skip to content

to_chars is wrong for negative values of int128_t #320

@pps83

Description

@pps83

sample code:

#define BOOST_INT128_ALLOW_SIGN_CONVERSION
#include <boost/int128.hpp>
#include <boost/int128/charconv.hpp>


void toChars(char* ptr, char* ptrEnd, boost::int128::int128_t i128)
{
    auto r = boost::charconv::to_chars(ptr, ptrEnd, i128);
    *r.ptr = '\0';
}

void toChars(char* ptr, char* ptrEnd, boost::int128::uint128_t u128)
{
    auto r = boost::charconv::to_chars(ptr, ptrEnd, u128);
    *r.ptr = '\0';
}

int main()
{
    boost::int128::int128_t i128 = -123;
    boost::int128::uint128_t u128 = 123;

    for (int i=1; i < 5; ++i)
    {
        i128 *= i;
        u128 *= i;

        char buf[64] = {};
        toChars(buf, buf + 64, i128); printf("i128 val: %s\n", buf);
        toChars(buf, buf + 64, u128); printf("u128 val: %s\n", buf);
    }
}

this is the output I get with VS2022:

i128 val: -18446744073709551493
u128 val: 123
i128 val: -18446744073709551370
u128 val: 246
i128 val: -18446744073709550878
u128 val: 738
i128 val: -18446744073709548664
u128 val: 2952

Metadata

Metadata

Assignees

Labels

BugSomething isn't working

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions