   Anyway to deal with overflow or underflow?
  Hi guys,I am trying to solve this problem of dealing with overflow and underflow.  If you want to solve say (a^2 + b^2)^(1/2), now ofcourse if the solution was to overflow/underflow, we can''t do much about that except throw error (or return infinity/0), but what if the solution was representable but the internal calculations aren''t?For example:  say ''a'' is the maximum value that is representable, now squaring it will give us an overflow, but if ''b'' was 0, then we get ''a'' as the final solution, which is representable.  But the software will return infinity (overflow).What are the things I can do to fix this?  I just need some hints to start off.  This is a bonus part of our assignment, and we are allowed to consult external sources. 
  Does it have to be fast?The first thing that pops up is simply to use larger datatypes, 64, or even 128-bit types.Otherwise, you could detect the over/underflows in the calculations by checking the carry-flag of the CPU, and for example splitting and shifting the result over several registers if it is set. This could mean you''ll have to use some advanced arithmetic-op''s once you''ve split a result, so you''re probably better off to use larger datatypes.Or combine them. Start out with a standard 32bit type. Once the first overflow is detected, convert them all to 64bit. Once that overflows, convert to your custom 128bit datatype, and so on.Making the custom types aren''t hard, it just requires some clever use of the stack.Oh, and you''re best off doing this in asm, since it presents op''s you don''t have in any highlevel-languages. --MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else. ;  <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt__control87.2c_._controlfp.asp">_controlfp</a>Or a great example:<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt__status87.2c_._statusfp.asp">_statusfp</a> ;  I guess you could use some trigonometry or taylor expansion to get the result, but since you don''t say what the course is about, it is hard to know what kind of solution to suggest. ;  The course is numerical analysis.  Thus, the solution should be done  through numerical methods.  I got an idea, but I have to spend some time working it out.  I still am looking for hints though.Thanks for trying guys, but its my fault that I didn''t specify how to get the solution. ;  I think the key to doing this is building a tree out of the expression, and then using this tree to solve it.  For example in your example you would build a tree looking something like this (stuff enclosed in [] is a node)    [^]   / \  [+] [1/2] /  \    [^]   [^]   / \    / \   [ a][2][ b][2]  The you would insert the apropiate values in a and b.  You'd then have to program in some rules so upon interation of the tree it would see you had b set as 0 and so b ^ 2 = 0 and removes that branch of the tree, leaving it to work out (a^2)^1/2 which it would then evaluate to a.At least I believe you could do it like that, the implementation may be rather complex, but it may help you out  [edit]Forums ate my beautiful ASCII tree  [edited by - Monder on February 3, 2004 3:32:49 PM] [edited by - Monder on February 3, 2004 3:33:41 PM] [edited by - Monder on February 3, 2004 3:33:59 PM];  Just scale a and b before the calculation. Scale it back afterwards. Simple.