Modify

Ticket #8711 (closed Bugs: fixed)

Opened 10 months ago

Last modified 8 months ago

cpp_int fails to construct correctly from minimum negative

Reported by: Jan Bouwer <JBouwer@…> Owned by: johnmaddock
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

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

Change History

Changed 10 months ago by Jan Bouwer <JBouwer@…>

A demonstration of the error.

Changed 10 months ago by Jan Bouwer <JBouwer@…>

A naive patch suggestion.

comment:1 Changed 10 months ago by johnmaddock

  • Status changed from new to assigned

Confirmed, investigating.

comment:2 Changed 10 months ago by johnmaddock

(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 10 months 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 8 months ago by johnmaddock

  • Status changed from assigned to closed
  • Resolution set to fixed
  • Milestone changed from To Be Determined to Boost 1.55.0
View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.