Boost logo

Boost Users :

Subject: Re: [Boost-users] What is the best way to obtain a constant one for any numeric type?
From: John Maddock (boost.regex_at_[hidden])
Date: 2014-05-19 14:57:11


> Is there any trait in Boost that allows me to get the value of one for
> any numeric type?
>
> What I'm looking for is something that would work like the following:
>
> boost::numeric_traits<int>::one; // 1
> boost::numeric_traits<long>::one; // 1L
> boost::numeric_traits<double>::one; // 1.0
> boost::numeric_traits<float>::one; // 1.0f
>
> And so on. Does it exist, or something equivalent? I've looked into
> NumericConversion, but I haven't quite found something similar. What I'm
> using now is something equivalent to the following:
>
> template<class Number>
> Number one()
> {
> return Number(1);
> }
>
> Maybe this is safe enough?

We have
http://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/constants.html

But it doesn't cover integer constants: I came to the conclusion that
any numeric type worth a damn would interoperate with basic integer
values anyway, and what's more maybe in a more efficient manner than
casting to a T would achieve (for example for many multiprecision types
MT, multiplying by an integer is way more efficient than multiplying by
an MT).

John.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net