Modify

Opened 4 years ago

Closed 4 years ago

#8711 closed Bugs (fixed)

cpp_int fails to construct correctly from minimum negative

Reported by: Jan Bouwer <JBouwer@…> Owned by: John Maddock
Milestone: Boost 1.55.0 Component: multiprecision
Version: Boost 1.53.0 Severity: Problem
Keywords: cpp_int negative minimum Cc:

Description

A cpp_int fails to construct correctly with the minimum negative value of a (platform dependent?) signed integral type.

Example:

template<typename T, class I>
void test_boost_multiprecision()
{
    T negMin = std::numeric_limits<T>::min();
    I test1(negMin), test2( negMin + 1 );
    --test2;
    
    assert( test1 == test2 ); // This fails on my platform for T = long
    assert( test1 != 0 );
    assert( test1 == negMin );
    assert( test2 != 0 );
    assert( test2 == negMin ); // This fails on my platform for T = long
}

test_boost_multiprecision<int64_t, boost::multiprecision::checked_cpp_int>(); // Fails on my platform

// The following hold for my platform: x86_64-apple-darwin12.4.0,
// using: Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
// with: -std=c++11 -stdlib=libc++
// static_assert(std::numeric_limits<long long>::min() == std::numeric_limits<int64_t>::min(), "Ok");
// static_assert(std::numeric_limits<long>::min() == std::numeric_limits<int64_t>::min(), "Ok");

Attachments (2)

boost_multiprecision_error_demo.cpp (1.4 KB) - added by Jan Bouwer <JBouwer@…> 4 years ago.
A demonstration of the error.
boost_multiprecision_max_negative.patch (733 bytes) - added by Jan Bouwer <JBouwer@…> 4 years ago.
A naive patch suggestion.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by Jan Bouwer <JBouwer@…>

A demonstration of the error.

Changed 4 years ago by Jan Bouwer <JBouwer@…>

A naive patch suggestion.

comment:1 Changed 4 years ago by John Maddock

Status: newassigned

Confirmed, investigating.

comment:2 Changed 4 years ago by John Maddock

(In [84897]) Add some typecasts to ensure conversion from negative values proceeds correctly. Fix masking of values during construction so overflow wraps around in unchecked fixed precision integers. Add tests for above changes. Refs #8711.

comment:3 Changed 4 years ago by Jan Bouwer <JBouwer@…>

This indeed fixes the issue I experienced (and others along the same line).

Thanks - your work (here and elsewhere) are much appreciated.

comment:4 Changed 4 years ago by John Maddock

Milestone: To Be DeterminedBoost 1.55.0
Resolution: fixed
Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain John Maddock.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.