1 2 // Copyright (C) 2008-2018 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 (see accompanying 4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 6 7 //[n1962_sqrt 8 #include <boost/contract.hpp> 9 #include <cmath> 10 #include <cassert> 11 lsqrt(long x)12long lsqrt(long x) { 13 long result; 14 boost::contract::check c = boost::contract::function() 15 .precondition([&] { 16 BOOST_CONTRACT_ASSERT(x >= 0); 17 }) 18 .postcondition([&] { 19 BOOST_CONTRACT_ASSERT(result * result <= x); 20 BOOST_CONTRACT_ASSERT((result + 1) * (result + 1) > x); 21 }) 22 ; 23 24 return result = long(std::sqrt(double(x))); 25 } 26 main()27int main() { 28 assert(lsqrt(4) == 2); 29 return 0; 30 } 31 //] 32 33