hi, beginner question

i am planning to write code for different values of a^b.
both a and b will vary in the program and both will be long long ints;
as my compiler wont allow pow(int,int) as it is ambiguous(dont know why)
either while have a as a double and cast it to LL int or just use multiplication
with a as LL int.

Sorry im rambling.

My question is how do i terminate the loop when a^b is
out of LL int range instead of it just returning me a garbage value and carrying on?
In other words b will have no limit will just break when a^b is too large.

p.s has to be int as i'm going to do modular operations on it.

Edited by frogboy77: n/a

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by frogboy77

>In other words b will have no limit will just break when a^b is too large.
Then it still has a limit. You can use an arbitrary length math package like GMP to get around the limitation. Alternatively, pow checks for out of range requests for you, sets errno appropriately, and returns HUGE_VAL:

``````errno = 0;
result = pow(a, b);

if (errno == ERANGE || result == HUGE_VAL || result == -HUGE_VAL) {
// Out of range!
}``````

sorry should have said that i'm very new at this(couple of months)
dont quite understand what you've posted
not willing to use external libraries(given i don't know how to)
how are "ERANGE" and "HUGE_VAL" defined

p.s i agree b will have a limit but it will be dependant on a
so what i meant was that b will have no stated limit in the loop
it will abort when a^b is too big

Edited by frogboy77: n/a

>not willing to use external libraries
You're already using the pow function, right? Just include <cerrno> for errno and ERANGE. HUGE_VAL is defined in <cmath>, which you're already including for pow. All of these are standard libraries. Technically, you could just check for HUGE_VAL and not have to use any extra headers:

``````result = pow(a, b);

if (result == HUGE_VAL || result == -HUGE_VAL) {
// Out of range
}``````

>(given i don't know how to)
That's a stupid reason. If you avoid things you don't know about because you don't know about them, you'll never progress beyond your current ability.

been at this 2 months taking one step at a time
using whats there first before thinking about importing more
not avoiding it just learning

That's a stupid reason. If you avoid things you don't know about because you don't know about them, you'll never progress beyond your current ability

thanks for the help i'll figure it out. remember when rebuking someone
"one must learn to walk before one can run."
if i was unwilling to learn new things i wouldn't have got here
:)

been at this 2 months taking one step at a time
using whats there first before thinking about importing more
not avoiding it just learning

thanks for the help i'll figure it out. remember when rebuking someone
"one must learn to walk before one can run."
if i was unwilling to learn new things i wouldn't have got here
:)

Your sentiments are correct, but a friendly note.

>>not willing to use external libraries(given i don't know how to)
This phrasing comes across as being unwilling, as opposed to cautious... Hence, Narue's comment.

Edited by Fbody: n/a

not unwilling just not ready yet

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.