Re: how to specify power of number
- From: Tim Prince <tprince@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 18 Apr 2008 18:21:54 -0700
Because math coprocessors are awfully fast now, I guess that pow(2.0,k) is nearly as fast as bit shifts and additions, but there is a bigger problem with using pow(2,x) to find exact powers of 2. It should not be unexpected for pow(2.0,24) to return 16777215.999999999 and if you assign that to an integer, it probably won't be what is wanted unless you are clever enough to round it -- ceil() won't help either because we might also have seen 16777215.0000001 or something like that. The Cephes collection of math functions {for instance} uses a ratio of a cubic polynomial divided by a quartic polynomial to form the approximation.
Likely, many implementations will actually recognize integral inputs (my C++ compiler definitely does this -- even sometimes complaining about ambiguity over argument type) but there is no reason to expect that to happen.
It's almost obligatory for an implementation of pow() to special case integral exponents, so as to handle negative operands, as well as provide accuracy. I thought Cephes did this.
In case this doesn't happen, it doesn't seem too far fetched to specify functions such as lrint(), supplying your own code if your position is that C99 isn't portable enough.
.
- Follow-Ups:
- Re: how to specify power of number
- From: user923005
- Re: how to specify power of number
- References:
- how to specify power of number
- From: Yanb
- Re: how to specify power of number
- From: Bartc
- Re: how to specify power of number
- From: Antoninus Twink
- Re: how to specify power of number
- From: Yanb
- Re: how to specify power of number
- From: Antoninus Twink
- Re: how to specify power of number
- From: user923005
- Re: how to specify power of number
- From: Antoninus Twink
- Re: how to specify power of number
- From: Dann Corbit
- how to specify power of number
- Prev by Date: A one stop website for all your programming language needs
- Next by Date: What are the minimum numbers of bits required to represent C std float and double components?
- Previous by thread: Re: how to specify power of number
- Next by thread: Re: how to specify power of number
- Index(es):
Relevant Pages
|