Skip to content
Snippets Groups Projects
Commit ff76b2ab authored by Markus Blatt's avatar Markus Blatt
Browse files

Added conversion to double

[[Imported from SVN: r5714]]
parent 8624cecb
No related branches found
No related tags found
No related merge requests found
......@@ -124,6 +124,7 @@ namespace Dune
//! export to other types
// operator unsigned int () const;
unsigned int touint() const;
double todouble() const;
friend class bigunsignedint<k/2>;
friend class std::numeric_limits<bigunsignedint<k> >;
......@@ -141,7 +142,9 @@ namespace Dune
// Constructors
template<int k>
bigunsignedint<k>::bigunsignedint ()
{ }
{
assign(0u);
}
template<int k>
bigunsignedint<k>::bigunsignedint (int y)
......@@ -175,6 +178,24 @@ namespace Dune
return (digit[1]<<bits)+digit[0];
}
template<int k>
inline double bigunsignedint<k>::todouble() const
{
int firstInZeroRange=n;
for(int i=n-1; i>=0; --i)
if(digit[i]!=0)
break;
else
--firstInZeroRange;
int representableDigits=std::numeric_limits<double>::digits/bits;
int lastInRepresentableRange=0;
if(representableDigits<firstInZeroRange)
lastInRepresentableRange=firstInZeroRange-representableDigits;
double val=0;
for(int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
val =val*(1<<bits)+digit[i];
return val*(1<<(bits*lastInRepresentableRange));
}
// print
template<int k>
inline void bigunsignedint<k>::print (std::ostream& s) const
......
......@@ -47,25 +47,8 @@ int main()
Dune::bigunsignedint<100> a, b, c;
a=100;
int ret=0;
if(a.touint()!=100)
{
std::cerr<<"wrong conversion"<<std::endl;
++ret;
}
b=3;
if(b.touint()!=3)
{
std::cerr<<"wrong conversion"<<std::endl;
++ret;
}
c=a/b;
if(c.touint()!=100/3)
{
std::cerr<<"wrong conversion"<<std::endl;
++ret;
}
std::cout<<a<<"/"<<b<<"="<<c<<std::endl;
try{
......@@ -74,13 +57,14 @@ int main()
b=0;
c=a/1;
std::cout<<a1<<"/"<<b1<<"="<<c1<<std::endl;
return ret;
a=1000000;
std::cout<<a.todouble()<<std::endl;
std::cout<<a.touint()<<std::endl;
b=a;
a=a*b*b;
std::cout<<a.todouble()<<std::endl;
}
catch(Dune::MathError e) {
std::cout<<e<<std::endl;
return 1;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment