Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#10104 closed Bugs (invalid)

program aborts with log1p(-2.0Q)

Reported by: Charles Karney <charles@…> Owned by: John Maddock
Milestone: To Be Determined Component: multiprecision
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

The math functions, e.g., log1p, for float128 do not handle parameters outside the normal domain. Thus sqrt(-2.0Q) produces a nan as does log1p(-2.0), but log1p(-2.0Q) crashes. (Similar remarks apply to hypot and probably other similarly defined functions.)

#include <iostream>
#include <cmath>
#include <boost/multiprecision/float128.hpp>
#include <boost/math/special_functions/log1p.hpp>

int main() {
  double x = -2;
  boost::multiprecision::float128 y = -2;
  
  std::cout << std::sqrt(x) << "\n" << std::log1p(x) << "\n";
  std::cout << sqrt(y) << "\n";
  std::cout << boost::math::log1p(y) << "\n";
}

produces

-nan
-nan
-nan
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::domain_error> >'
  what():  Error in function boost::math::log1p<N5boost14multiprecision6numberINS0_8backends16float128_backendELNS0_26expression_template_optionE0EEE>(N5boost14multiprecision6numberINS0_8backends16float128_backendELNS0_26expression_template_optionE0EEE): log1p(x) requires x > -1, but got x = -2.
Aborted (core dumped)

Change History (3)

comment:1 by John Maddock, 8 years ago

Resolution: invalid
Status: newclosed

It doesn't crash, it throws an exception which you don't catch. This is documented behavior for boost::math::some_special_function and can be controlled via policies: http://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/math_toolkit/pol_tutorial.html

comment:2 by Charles Karney <charles@…>, 8 years ago

OK. Sorry for not researching this enough. Minor quibbles remain:

  1. log1p(-2.0Q) -> inf and not nan
  2. log1p(-1.0Q) -> inf and not -inf

comment:3 by John Maddock, 8 years ago

Not so minor, but I'm unable to reproduce (admittedly using current develop), this:

   std::cout << boost::math::log1p(float128(-1)) << std::endl;
   std::cout << boost::math::log1p(float128(-2)) << std::endl;

Outputs

-inf
nan

as expected.

Note: See TracTickets for help on using tickets.