Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 pow() function
Index -> Programming, C -> C Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dragan




PostPosted: Sat Apr 12, 2008 4:45 pm   Post subject: RE:pow() function

Every integer can't be converted in float with 100% precision, because of that, you get in some cases errors, that is problem in processor, 64bit processors have more bits for representing float point number and errors is less possible.
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: Sat Apr 12, 2008 6:45 pm   Post subject: Re: pow() function

This is one of those areas where well intended and common sense advice can be a little dangerous. I'd say I understand float and the sign bit, 8 bit exponent and 23 bit mantissa. But there are devils in the details. normalized, denormalized, negative zero to name a few. littlewontons; Take a look at Layhey Float for one example of some of the perils of blindly converting float to int. It should give you the information you need to figure out the technical details of what you are seeing. google c float representation for a boatload more links to the technicals.

I'm sadden that with gigahertz machines, Intel and friends haven't moved to a base-10 internal representaiton and be done with the base-2 floating point precision edge case approximations for once and for all.

OneOffDriveByPoster gave me a link to info on IEEE 745r, but I was disappointed with the trends. Processor speeds are now at a point where the need to do pure binary arithmetic should be becoming a thing of the past. Oh well. Blame QWERTY and human resistance to change I guess. See the Floating point precision thread for his link.

As pointed out on the Lahey site (they do Fortran, and Fortran does computer math), a quick example is 0.01 We know it as 1/100, 32bit base-2 floats represent this as 10737418/1073741824 or 0.00999999... which rounds nicely to 0.01 (until you scale it up a few orders of magnitude where problems can start to occur). Knowing what is going on and explaining what is going on are two different things. Like pi, I've always used 22/7 in "real life" taking to "normal people" or 355/113 talking with a building contractor, (or at Waterloo you impress your friends memorizing the first n digits) but I wouldn't do that working for NASA.

I guess all the above old-guy babbling is just to say; read up, try and understand it and take care explaining it to those with less experience. Devils in details that most humans care little about or are capable of remembering in casual converstions, but engineers have to, when they have to.

Cheers
md




PostPosted: Sat Apr 12, 2008 8:11 pm   Post subject: RE:pow() function

C uses IEEE floats; so there isn't a whole lot of unknowns.

The error is almost certainly being caused by rounding issues, as the float cannot completely accurately represent the number required. It's not a problem or a bug, it's simply a limit of the data format. A Double might have the same issue though to a lesser degree.

btiffin, the reason we do binary math is because it's so damned fast. Decimal math is complex and significantly slower. Why do something slow when in the majority of cases you can do it 1000x faster in base 2 and get the same answer - and when you don't the fix is fairly quick (range checking instead of equality). Just because computers are constantly getting faster does not mean that their speed should be wasted doing needlessly complex things.
btiffin




PostPosted: Sat Apr 12, 2008 10:24 pm   Post subject: Re: RE:pow() function

md @ Sat Apr 12, 2008 8:11 pm wrote:
C uses IEEE floats; so there isn't a whole lot of unknowns.
btiffin, the reason we do binary math is because it's so damned fast. Decimal math is complex and significantly slower. Why do something slow when in the majority of cases you can do it 1000x faster in base 2 and get the same answer - and when you don't the fix is fairly quick (range checking instead of equality). Just because computers are constantly getting faster does not mean that their speed should be wasted doing needlessly complex things.


Agree. And disagree. Humans are decimal. Spending a few hundred cycles of a gigahertz chip to make computing more humanitarian, opening access to people that don't want to be programmers (the vast majority of humans by the way), is a good use of processing power imho. I'm old. My TRS/80 Model I ran a debugger just as fast as the IDE's of today. Chips that run at 2.4 billion hertz versus 6 million hertz just a scant 30 years ago. 400 times faster. I don't get 400 more things done per minute, so why not make it easy for my Mom to do math on a computer? Just my opinion. Not really necessary, but I'd like to see programming more open to the masses. It's why I use REBOL. It has humanistic qualities to it. I spent most of my programming career using polyFORTH. Enjoyed the bit twiddling then, less so now, except when I get the odd urge for it. I usually try to hand assemble a few instructions for an hour or two or read a chip spec ... the urge goes away.

And who doesn't like needless complexity? Smile Vista needs a lot of juice. Runs the same software apps that XP did only with ??? (I dunno) no value added there imho for the computing power required. But that's ok. It is prettier. The human side of the equation.

Why not have an industry standard base-10 math in a chip by now? It doesn't need to be the default, just an option.

10.3 is an exact decimal value. 103 / 10. Not so in IEEE 745r. Not that computers shouldn't have fast math capabilities, there should be an option by now for human math. I was hoping to see this filtering into IEEE specs.

Cheers
md




PostPosted: Sat Apr 12, 2008 10:47 pm   Post subject: RE:pow() function

The problem is that almost everything a CPU does is math - most of it having no relation to user IO. If you did all that math in Decimal the computer would be so slow as to be WORSE then a 33mhz 386, and that's just not acceptable.

It's a trade off between an arbitrary level of precision, and speed. And speed is infinitely more important. Especially since decimal based computers (if done in hardware) are ridiculously more expensive to manufacture; and if done in software are simply slower.

Incidentally, humans are not decimal - if you ask any programmer who has a decent amount of experience I am willing to bet most can do base 16 and base 2 math in their heads easily. Base 10 is very useful and simple for some things, base 2 is better for others. Both are in the end learned skills however.

Oh, and IEEE is an engineering group - not human. Clearly you're looking in the wrong place Wink
btiffin




PostPosted: Sat Apr 12, 2008 11:46 pm   Post subject: Re: RE:pow() function

md @ Sat Apr 12, 2008 10:47 pm wrote:
Oh, and IEEE is an engineering group - not human. Clearly you're looking in the wrong place Wink


Ahh, you got me. I had to smile out loud. Smile

md @ Sat Apr 12, 2008 10:47 pm wrote:
The problem is that almost everything a CPU does is math - most of it having no relation to user IO. If you did all that math in Decimal the computer would be so slow as to be WORSE then a 33mhz 386, and that's just not acceptable.


I'll disagree again though. CPUs do integer math. The base doesn't matter then. An FPU in an exception, hardware or software emulation. Why not let it chew for math libraries. I'll wait. For those times when I want decimal accuracy and precision, in the base I was taught on Sesame Street.

You are making good points. We may differ on our opinion of what makes for "good" computing. I lean more and more to human than digital now-a-days. But I'll also admit, a few years ago, I'd disagree with what I'm now saying and raspberry the old-guy for "not getting it".

Cheers
littlewontons




PostPosted: Sun Apr 13, 2008 12:45 pm   Post subject: RE:pow() function

Thanks guys, I guess i'll have to avoid using the pow() function for integers. >< I got some of the topics, but you guys are so experienced, i have no idea what some of you are talking about. lol. i'll write my own function then. Thanks alot. =)
btiffin




PostPosted: Sun Apr 13, 2008 2:55 pm   Post subject: Re: RE:pow() function

littlewontons @ Sun Apr 13, 2008 12:45 pm wrote:
Thanks guys, I guess i'll have to avoid using the pow() function for integers. >< I got some of the topics, but you guys are so experienced, i have no idea what some of you are talking about. lol. i'll write my own function then. Thanks alot. =)


Sorry littlewontons, that was not the intent at all. Check to see if your C implementation has pow10(), and if so, run some code experiments with it. (if you are still doing to the power of 10 in the code that is). But as has been mentioned, an int based to-the-power function is quite easy to write, but you're upper limit will be restricted to 32 or 64 bit integer numeric range. Quite limiting. 10 to the 10th power overflows a 32 bit int. 10 to the 19th power overflows a signed 64 bit integer.

Best of luck, and apologies for hijacking your thread. Tsk Tsk to us (well, me at least).

Cheers
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sun Apr 13, 2008 4:24 pm   Post subject: Re: RE:pow() function

btiffin @ 2008-04-12, 11:46 pm wrote:
md @ Sat Apr 12, 2008 10:47 pm wrote:
The problem is that almost everything a CPU does is math - most of it having no relation to user IO. If you did all that math in Decimal the computer would be so slow as to be WORSE then a 33mhz 386, and that's just not acceptable.


I'll disagree again though. CPUs do integer math. The base doesn't matter then. An FPU in an exception, hardware or software emulation. Why not let it chew for math libraries. I'll wait. For those times when I want decimal accuracy and precision, in the base I was taught on Sesame Street.

You are making good points. We may differ on our opinion of what makes for "good" computing. I lean more and more to human than digital now-a-days. But I'll also admit, a few years ago, I'd disagree with what I'm now saying and raspberry the old-guy for "not getting it".

Cheers


Off topic by a long ways now, but who cares - it's not like a mod will stop it Wink

You're right, CPUs DO do integer math. Which could indeed be done using decimal numbers. HOWEVER, in a system based on on/off, how do you represent base 10 numbers? Either you use 10 on/off switches, or you do it one digit at a time in base2 (5 bits per base10 digit). Neither of those methods is at all efficient, and since all of the power of computers comes from their efficiency (which leads to speed) both those solutions end up being considerably slower and more costly then a base2 CPU. It really ends up being a trade off between performance and marginal utility, as the majority of the computations performed by a computer are perfectly accurate in base2.

Incidentally, it is entirely possible to do fractional math which can be as precise as you need (and in base2). It does require you to write some code to do it for you in the majority of languages however. That trade off, doing base2 math and using special data structures for base10, is in fact much faster and more efficient then any base10 CPU could be. It is also more then twice as slow as software based IEEE floating point numbers; and more then twice as slow as hardware IEEE based floating point numbers.
OneOffDriveByPoster




PostPosted: Sun Apr 13, 2008 4:37 pm   Post subject: Re: RE:pow() function

md @ Sun Apr 13, 2008 4:24 pm wrote:
You're right, CPUs DO do integer math. Which could indeed be done using decimal numbers.
I think the point was that decimal floating point in HW (IBM does have it Smile ) would be nice. No one is asking for the CPU integer arithmetic to be decimal--no accuracy or precision issues there (just range issues).
btiffin




PostPosted: Sun Apr 13, 2008 4:49 pm   Post subject: Re: RE:pow() function

md @ Sun Apr 13, 2008 4:24 pm wrote:

You're right, CPUs DO do integer math. Which could indeed be done using decimal numbers. HOWEVER, in a system based on on/off, how do you represent base 10 numbers? Either you use 10 on/off switches, or you do it one digit at a time in base2 (5 bits per base10 digit). Neither of those methods is at all efficient, and since all of the power of computers comes from their efficiency (which leads to speed) both those solutions end up being considerably slower and more costly then a base2 CPU. It really ends up being a trade off between performance and marginal utility, as the majority of the computations performed by a computer are perfectly accurate in base2.

Incidentally, it is entirely possible to do fractional math which can be as precise as you need (and in base2). It does require you to write some code to do it for you in the majority of languages however. That trade off, doing base2 math and using special data structures for base10, is in fact much faster and more efficient then any base10 CPU could be. It is also more then twice as slow as software based IEEE floating point numbers; and more then twice as slow as hardware IEEE based floating point numbers.


Agree with almost all of this. My point is, engineers (IEEE) should be pumping out a spec for a base-10 FPU mode. CPU needs to stay binary until the Quantum Computers and/or the Japanese trinary machines become commonplace. They could implement it as BCD or whoosy/whatsey decimal encoding with layers of chaos theory and probabilities (let those that care, care, those that don't, don't), unencumbered by the need to pack things into 4 bytes (a big deal, not too too many years ago, but a time that has passed). Give a reasonable human interface to it and then move on to the next big thing ... 3D interactive porn, err, I mean cartoons. Joking kids. Not that porn hasn't funded a lot of what is today's Internet; cartoons are actually better for you. Smile

Apologies once again for dilluting littlewontons thread, can't seem to help myself. Kinda like umm, cartoons.

Cheers
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 26 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: