Bill Cunningham wrote:
>
"santosh" <sa*********@gmail.comwrote in message
news:fr**********@registered.motzarella.org...
>Bill Cunningham wrote:
>>>
Describe what you are actually trying to do. Do you want to
generate 128 bit integer values? What for? What are you actually
going to do with them?
[...]
>> When you said about my first code it wasn't compilable do you
see what I was trying to do ?
No.
>> I could use errno.h and generate error codes.
Yes. That's the proper method.
>> When I get 0 I don't know if there's a overflow or I'm
coding wrong.
Both in your case. Use long double. If you want to represent the
value in an integer then you need a bignum package like GMP.
>> I was trying to add error checking to the second
code I wrote.
But it was wrong.
Thanks Santosh. But out of curiosity before errno.h how would
error
checking or a diagnostic method have been used in this situation?
Actually the Standard does not specify whether pow/powf/powl will set
errno if an overflow or underflow occurs. On my system for example
errno is not set for overflow of pow(2, 1288), but it *is* set for the
call powl(2, 12888899999999). It's a quality of implementation issue.
All it says is that a "range error" *may* occur if the result is out of
range for the result type or a "domain error" will occur if the first
argument is finite and negative and the second is finite and not an
integer value.
So for pow/powf/powl there is no sure way to detect a range error. It
depends on what your compiler does. You can however, set errno to zero
before the call to pow/powf/powl and then test errno to see whether it
is set to ERANGE. You might also want to output the system's error
message through perror or sterror.
Actually C99 has added a lot of functions and macros that you can use to
test whether additional guarantees are made by your implementation. For
example you can test MATH_ERRNO to see whether a math function sets
errno to a non-zero value. You can also use it in conjunction with the
macro math_errhandling. There are many other details that you probably
don't want to know.
My advice is if there is a chance that your calculations may overflow or
underflow a long double, then use an extended precision math package
like GNU GMP. It reduces portability, but it might not matter in your
case.
My
long doubles are 12 bytes on this system while double are 8. The
number generated once I cleared up the printf specifier problem had
many zeros on the end so a long double is no doubt needed.
A double of eight bytes is most likely sufficient for calculating 2^128.
Check with DBL_MAX just to be sure.
<snip>