[MPL.Math] compile-time double macro and printing

Hi, I am trying to simply print a compile-time double using gcc 4.0.0 on Sun. The following prints out : 1.9036e+185 #include <boost/metamath/double.hpp> #include <boost/metamath/double_macros.hpp> #include <iostream> int main() { typedef BOOST_DOUBLE(3.3) D; D d; std::cout << (double)d << std::endl; return 0; } Am I doing something wrong? Chris

--- Chris Weed wrote:
Hi, I am trying to simply print a compile-time double using gcc 4.0.0 on Sun. The following prints out : 1.9036e+185
#include <boost/metamath/double.hpp> #include <boost/metamath/double_macros.hpp> #include <iostream>
int main() { typedef BOOST_DOUBLE(3.3) D; D d; std::cout << (double)d << std::endl; return 0; }
Am I doing something wrong? Chris
The most up-to-date version of MPL.Math is now at the Boost vault <http://boost-consulting.com/vault/>. The program should look like: #include <boost/mpl/math/double.hpp> int main() { typedef BOOST_MPL_MATH_DOUBLE(3.3) D; D d; std::cout << d << std::endl; return 0; } You don't need the cast because the implicit conversion already returns a double. If you still encounter problems, let me know. Cromwell D. Enage __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

On 12/19/05, Chris Weed <chrisweed@gmail.com> wrote:
Hi, I am trying to simply print a compile-time double using gcc 4.0.0 on Sun. The following prints out : 1.9036e+185
#include <boost/metamath/double.hpp> #include <boost/metamath/double_macros.hpp> #include <iostream>
int main() { typedef BOOST_DOUBLE(3.3) D; D d; std::cout << (double)d << std::endl; return 0; }
Am I doing something wrong? Chris
This has probably got something to do with the way I translate a mpl::double_ to a double. I use a union to represent the mapping between a double and the mantissa,exponent and sign (represented as integers) This mapping is most likely different for different systems, so we need to either device a better scheme for converting from compile-time to runtime double, or use different mappings for different systems. The relevant code is found in: boost\mpl\math\double_\aux_\get_value.hpp struct encoding { unsigned long mantissa2; unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; }; union converter_ { double value; encoding encode; }; template <typename T> double get_value(const T& arg) { converter_ converter; converter.encode.sign = T::sign; converter.encode.exp = T::exponent + 1023; converter.encode.mantissa1 = T::mantissa::part1 >> 10; converter.encode.mantissa2 = (T::mantissa::part1 << 22) | (T::mantissa::part2 >> 9); return converter.value; } Try fiddling with the order of the elements in the encoding struct, e.g. struct encoding { unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; //Move this to the end unsigned long mantissa2; }; On the other hand, this encoding is probably documented somewhere... Regards, Peder
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hi, The arguments in reverse order (shown below) worked for my Sun system. struct encoding { unsigned long sign:1; unsigned long exp:11; unsigned long mantissa1:20; unsigned long mantissa2; }; Thanks, Chris On 12/21/05, Peder Holt <peder.holt@gmail.com> wrote:
On 12/19/05, Chris Weed <chrisweed@gmail.com> wrote:
Hi, I am trying to simply print a compile-time double using gcc 4.0.0 on Sun. The following prints out : 1.9036e+185
#include <boost/metamath/double.hpp> #include <boost/metamath/double_macros.hpp> #include <iostream>
int main() { typedef BOOST_DOUBLE(3.3) D; D d; std::cout << (double)d << std::endl; return 0; }
Am I doing something wrong? Chris
This has probably got something to do with the way I translate a mpl::double_ to a double. I use a union to represent the mapping between a double and the mantissa,exponent and sign (represented as integers) This mapping is most likely different for different systems, so we need to either device a better scheme for converting from compile-time to runtime double, or use different mappings for different systems.
The relevant code is found in: boost\mpl\math\double_\aux_\get_value.hpp
struct encoding { unsigned long mantissa2; unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; };
union converter_ { double value; encoding encode; };
template <typename T> double get_value(const T& arg) { converter_ converter; converter.encode.sign = T::sign; converter.encode.exp = T::exponent + 1023; converter.encode.mantissa1 = T::mantissa::part1 >> 10; converter.encode.mantissa2 = (T::mantissa::part1 << 22) | (T::mantissa::part2 >> 9); return converter.value; }
Try fiddling with the order of the elements in the encoding struct, e.g. struct encoding { unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; //Move this to the end unsigned long mantissa2; };
On the other hand, this encoding is probably documented somewhere...
Regards, Peder
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

I suspect this is probably just a big/little endian problem. I don't know if boost has a typedef for this. Chris On 12/21/05, Chris Weed <chrisweed@gmail.com> wrote:
Hi, The arguments in reverse order (shown below) worked for my Sun system.
struct encoding { unsigned long sign:1; unsigned long exp:11; unsigned long mantissa1:20; unsigned long mantissa2; };
Thanks, Chris
On 12/21/05, Peder Holt <peder.holt@gmail.com> wrote:
On 12/19/05, Chris Weed <chrisweed@gmail.com> wrote:
Hi, I am trying to simply print a compile-time double using gcc 4.0.0 on Sun. The following prints out : 1.9036e+185
#include <boost/metamath/double.hpp> #include <boost/metamath/double_macros.hpp> #include <iostream>
int main() { typedef BOOST_DOUBLE(3.3) D; D d; std::cout << (double)d << std::endl; return 0; }
Am I doing something wrong? Chris
This has probably got something to do with the way I translate a mpl::double_ to a double. I use a union to represent the mapping between a double and the mantissa,exponent and sign (represented as integers) This mapping is most likely different for different systems, so we need to either device a better scheme for converting from compile-time to runtime double, or use different mappings for different systems.
The relevant code is found in: boost\mpl\math\double_\aux_\get_value.hpp
struct encoding { unsigned long mantissa2; unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; };
union converter_ { double value; encoding encode; };
template <typename T> double get_value(const T& arg) { converter_ converter; converter.encode.sign = T::sign; converter.encode.exp = T::exponent + 1023; converter.encode.mantissa1 = T::mantissa::part1 >> 10; converter.encode.mantissa2 = (T::mantissa::part1 << 22) | (T::mantissa::part2 >> 9); return converter.value; }
Try fiddling with the order of the elements in the encoding struct, e.g. struct encoding { unsigned long mantissa1:20; unsigned long exp:11; unsigned long sign:1; //Move this to the end unsigned long mantissa2; };
On the other hand, this encoding is probably documented somewhere...
Regards, Peder
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

| -----Original Message----- | From: boost-bounces@lists.boost.org | [mailto:boost-bounces@lists.boost.org] On Behalf Of Peder Holt | Sent: 21 December 2005 22:38 | To: boost@lists.boost.org | Subject: Re: [boost] [MPL.Math] compile-time double macro and printing | | On the other hand, this encoding is probably documented somewhere... | | Regards, | Peder | http://nssdc.gsfc.nasa.gov/nssdc/formats/ http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html might help. Paul -- Paul A Bristow Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB Phone and SMS text +44 1539 561830, Mobile and SMS text +44 7714 330204 mailto: pbristow@hetp.u-net.com http://www.hetp.u-net.com/index.html http://www.hetp.u-net.com/Paul%20A%20Bristow%20info.html Paul
participants (4)
-
Chris Weed
-
Cromwell Enage
-
Paul A Bristow
-
Peder Holt