Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucsd!usc!sdd.hp.com!decwrl!nsc!amdahl!netcom!mcmahan From: mcmahan@netcom.UUCP (Dave Mc Mahan) Newsgroups: comp.sys.m68k Subject: Re: ML FP Message-ID: <11865@netcom.UUCP> Date: 10 Jul 90 03:11:34 GMT References: <4739@munnari.oz.au> Organization: Dave McMahan @ NetCom Services Lines: 98 In a previous article, krooglik@gondwana10.ecr.mu.oz (Alex KROOGLIK) writes: > > Can anyone help me. I have been programming in 68K on the Amiga for >around 2 years now and I have finally taken the plunge and tried to write >some FP software. However, I am finding this extremely diffcult. I have >considered using BCD but I find it too slow. What I would like to know >is if anyone has written FP software that is SUPER accurate. 128 bits >doesn't particularly concern me. What I basically want to know is: > > 1. How do I turn a decimal number (eg 1.23123484) into Binary. I'm not sure if this is what you want, but it works for me. This routine was written off the top of my head and may have a few syntax errors. The concept is quite simple, though. I assume that when you say "decimal number" you mean a string that contains a decimal number, which you then wish to turn into an internal floating point form. Integer decimal numbers are a trivial subset of the following routine. float base10_to_float(input) char *input; { int decimal_pt_seen; float output_value, scale_factor; decimal_pt_seen = FALSE; output_value = 0.0; scale_factor = 0.1; while (*input) { switch (*input) { case '.' : if (!decimal_pt_seen) { decimal_pt_seen = TRUE; } else { puts("Too many decimal points in your value\n"); return -999999.99999; /* Or whatever signifies an error */ } break; case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : if (!decimal_pt_seen) { return_value = return_value * 10.0; return_value = return_value + (*input - '0'); } else { return_value = return_value + scale_factor * (*input - '0'); scale_factor = scale_factor / 10.0; } break; default : printf("Unrecognized character value in the string.\n"); return -999999.99999; /* Or whatever signifies an error */ } input++; } return return_value; } > 2. How do I then convert the binary into FP (ie Normalise it) To convert to the internal format of the floating point chip, you need to know the specific bit patterns and length of the chip in use. There are bits for sign and representation of infinities, bits for the mantissa and bits for the exponent. Each chip family lays these out in a different form that they feel gives the best results for what they have optimized to. I assume you mean the format used by the 68881/2 chip family. I can't seem to find my book, so I will not be able to help you out on this one. Obviously, the best way is if the compiler you are using (or are you using an assembler?) supports the format of the chip you are using. Otherwise, you need to do some major writing of routines that convert between ASCII, FP format, and integer formats. For super accurate stuff (many digits of precision) there are several approaches. From you post, it seems that speed is essential. Without knowing more about your specific task, I'm afraid I can't help you. > Alex Krooglik -dave