$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r71271 - trunk/libs/math/example
From: pbristow_at_[hidden]
Date: 2011-04-15 09:37:22
Author: pbristow
Date: 2011-04-15 09:37:21 EDT (Fri, 15 Apr 2011)
New Revision: 71271
URL: http://svn.boost.org/trac/boost/changeset/71271
Log:
First nonfinite example nonfinite_signaling_NaN.cpp.
Added:
   trunk/libs/math/example/nonfinite_signaling_NaN.cpp   (contents, props changed)
Added: trunk/libs/math/example/nonfinite_signaling_NaN.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/example/nonfinite_signaling_NaN.cpp	2011-04-15 09:37:21 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,174 @@
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// Copyright (c) 2006 Johan Rade
+// Copyright (c) 2011 Paul A. Bristow
+
+/*!
+\file
+\brief Tests of nonfinite signaling NaN loopback.
+
+\detail  nonfinite signaling NaN 
+test outputs using nonfinite facets
+(output and input) and reads back in, and checks if loopback OK.
+
+Not expected to work on all platforms (if any).  But shows that on MSVC,
+this legacy locale can ensure a consistent quiet NaN input from representations
+"1.#QNAN", "1.#SNAN" and "1.#IND"
+
+*/
+
+#ifdef _MSC_VER
+#   pragma warning(disable : 4702)
+#endif
+
+#include <boost/math/special_functions/nonfinite_num_facets.hpp>
+using boost::math::nonfinite_num_get;
+using boost::math::nonfinite_num_put;
+
+#include <iostream>
+using std::cout;
+using std::endl;
+
+#include <locale>
+using std::locale;
+
+#include <string>
+using std::string;
+
+#include <sstream>
+  using std::stringstream;
+  using std::istringstream;
+
+#include <limits>
+using std::numeric_limits;
+
+int main()
+{
+  locale default_locale; // Current global locale.
+  // Try to use the default locale first.
+  // On MSVC this doesn't work.
+
+  { // Try Quiet NaN
+    stringstream ss; // Both input and output.
+    ss.imbue(default_locale); // Redundant, of course.
+    string infs; 
+    if(numeric_limits<double>::has_quiet_NaN)
+    {  // Make sure quiet NaN is specialised for type double.
+      double qnan = numeric_limits<double>::quiet_NaN();
+      ss << qnan; // Output quiet_NaN.
+      infs = ss.str();  // 
+    }
+    else
+    { // Need to provide a suitable string for quiet NaN.
+     infs =  "1.#QNAN"; 
+      ss << infs;
+    }
+ 	  double r;
+	  ss >> r; // Read back in.
+
+    cout << "quiet_NaN output was " << infs << endl; // "1.#QNAN"
+    cout << "quiet_NaN input was " << r << endl; // "1"
+  }
+
+  { // Try Signaling NaN
+    stringstream ss; // Both input and output.
+    ss.imbue(default_locale); // Redundant, of course.
+    string infs; 
+    if(numeric_limits<double>::has_signaling_NaN)
+    {  // Make sure signaling NaN is specialised for type double.
+      double qnan = numeric_limits<double>::signaling_NaN();
+      ss << qnan; // Output signaling_NaN.
+      infs = ss.str();  // 
+    }
+    else
+    { // Need to provide a suitable string for signaling NaN.
+     infs =  "1.#SNAN"; 
+      ss << infs;
+    }
+ 	  double r;
+	  ss >> r; // Read back in.
+
+    cout << "signaling_NaN output was " << infs << endl; // "1.#QNAN" (or "1.#SNAN"?)
+    cout << "signaling_NaN input was " << r << endl; // "1"
+  }
+
+  // Create legacy_locale and store the nonfinite_num_get facet (with legacy flag) in it.
+  locale legacy_locale(default_locale, new nonfinite_num_get<char>(boost::math::legacy));
+  // Note that the legacy flag has no effect on the nonfinite_num_put output facet.
+
+  cout << "Use legacy locale." << endl;
+  
+  { // Try infinity.
+    stringstream ss; // Both input and output.
+    ss.imbue(legacy_locale);
+    string infs; 
+    if(numeric_limits<double>::has_infinity)
+    {  // Make sure infinity is specialised for type double.
+      double inf = numeric_limits<double>::infinity();
+      ss << inf; // Output infinity.
+      infs = ss.str();  // 
+    }
+    else
+    { // Need to provide a suitable string for infinity.
+     infs =  "1.#INF"; 
+      ss << infs;
+    }
+ 	  double r;
+	  ss >> r; // Read back in.
+
+    cout << "infinity output was " << infs << endl; // "1.#INF"
+    cout << "infinity input was " << r << endl; // "1.#INF"
+  }
+
+  { // Try input of "1.#SNAN".
+	  //double inf = numeric_limits<double>::signaling_NaN(); // Assigns "1.#QNAN" on MSVC.
+    // So must use explicit string "1.#SNAN" instead.
+    stringstream ss; // Both input and output.
+    ss.imbue(legacy_locale);
+    string s = "1.#SNAN";
+
+	  ss << s; // Write out.
+	  double r;
+  
+    ss >> r; // Read back in.
+
+    cout << "SNAN output was " << s << endl; // "1.#SNAN"
+    cout << "SNAN input was " << r << endl;  // "1.#QNAN"
+  }
+
+  { // Try input of "1.#IND" .
+    stringstream ss; // Both input and output.
+    ss.imbue(legacy_locale);
+    string s = "1.#IND";
+	  ss << s; // Write out.
+	  double r;
+	  ss >> r; // Read back in.
+
+    cout << "IND output was " << s << endl; // "1.#IND"
+    cout << "IND input was " << r << endl;  // "1.#QNAN"
+  }
+
+} // int main()
+
+/*
+
+Output:
+  nonfinite_signaling_NaN.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\nonfinite_signaling_NaN.exe
+
+  quiet_NaN output was 1.#QNAN
+  quiet_NaN input was 1
+  signaling_NaN output was 1.#QNAN
+  signaling_NaN input was 1
+  Use legacy locale.
+  infinity output was 1.#INF
+  infinity input was 1.#INF
+  SNAN output was 1.#SNAN
+  SNAN input was 1.#QNAN
+  IND output was 1.#IND
+  IND input was 1.#QNAN
+
+
+*/
+