CompuServe Thread

#BEGENNER HELP" again!

29 messages in this thread
#48455From: Douglas HornalOct 11, 1995 1:45 AM
Could someone pleese tell me what is wrong with the following program (it is in C…compiled with the manix/Aztec compilar-linker (commersial)) main() { float number; printf("hello!\n"); number = 1000000; while(number >= 0) { printf("%f\n", number); number = number – 1.; } } It is supposed to open up a window on the workbench (or if run from the CLI, it will use the CLI) and count down from one million.(and say hello!) the problem is that when it is subtracting, it is not accurite! it says "1000000 999999.253964 999998.175283 999997.056238…" Where did the decimal point come from? I know the amiga can do proper sutraction! If anyone has any ideas, could thay send them! (I am the same person who, a few weeks ago, was asking about how to get started in "C". I found the Aztec compilar 2nd hand with the book "Amiga C for beginners" and many other refrances.
#48458From: Jim ButterfieldOct 11, 1995 12:42 PM
You're just learning about the charms of floating point. You may change your variable to an integer type (I suggest LONG or UNISGNED LONG), and the fractional part will go away. You may make your floating point number DOUBLE (or LONG FLOAT) and the fractional part will become virtually insignificant. You can make the fractional part SEEM to go away by specifying more detail in your printf() conversion specification, thus: printf("%7.0f\n",number); ,, but most importantly, you've learned a few important things: (1) don't depend on floating point numbers to be exact; (2) use DOUBLE if you need a lot of places of accuracy; (3) so long as you're aware the garbage is there, feel free to hide it; (4) be VERY careful in testing floating point numbers for equality [you're OK on this one, since you coded ( number >= 0 ) ] –Jim
#48536From: Douglas HornalOct 18, 1995 12:03 AM
I'll try that. On a side note, CAN the amiga do perfact addition, subtraction, * and / with decimal without errors? BTW, i tryed printf("%7.000000\n, number);…It dident work. oh well. Thanks for the pointer! 🙂 -Daniel (useing my dads account)
#48537From: Greg Comeau@Comeau CmptgOct 18, 1995 9:28 AM
>I'll try that. On a side note, CAN the amiga do perfact addition, subtraction, >* and / with decimal without errors? Yes, but everything is of course finite. > BTW, i tryed printf("%7.000000\n, >number);…It dident work. oh well. Thanks for the pointer! Remember that printf needs to _convert_ the float number to a strings before/while printing it, and doesn't just print it raw. Please make sure that you are using whatever respective math library is appropriate with your compiler.
#48538From: Dan NamayOct 18, 1995 11:23 AM
> I'll try that. On a side note, CAN the amiga do perfact addition, subtraction, * and / with decimal without errors? BTW, i tryed printf("%7.000000\n, number);…It dident work. oh well. Thanks for the pointer! 🙂 -Daniel (useing my dads account) Dan, Try: printf (%7.6f\n", variable); The "7" is the number of Whole Number places the computer prints. The "6" is the number of Decimal places the computer prints. As to "perfect" subtraction, * and / with decimal without errors, I'm not too sure about that. Wolf Pup – Howlin' at the Net, Six-Packin' & Diggin'
#48542From: Jim ButterfieldOct 18, 1995 12:58 PM
>> CAN the Amiga do perfect addition, subtraction … with decimal .. ? It's a matter of definition. Can any entity in the finite universe divide 10 by 3, in decimal, perfectly? Even with 24 megabytes of memory, the "3" digits would march on and there would be a loss of "perfection" somewhere. If you want "usual" decimal accuracy, you can indeed carry your numbers in decimal, although the C language won't be too helpful in the advanced arithemetic area .. likely the best approach would be to keep the value as an integer, and the power-of-10 as a separate related value. In many practical cases, you can just do LONG computations, on the assumption that you will have a large number of significant figures and can trim out any error. In assembler, you can do virtually what you want. Want to calculate in decimal mode to about 500 significant digits? Go for it; just allocated the appropriate space for each value and write the special-purpose arithmetic operations. And you can do this in C, too, of course: no reason why a number can't be an array of however many digits you wish to define. It's a major breakthrough to realize that computers, despite their popular reputation, are not always exact in the way they handle numbers. A good book on numeric methods will emphasize how the programmer needs to take accuracy (and inaccuracy) into account during calculations. –Jim
#48544From: Greg Comeau@Comeau CmptgOct 18, 1995 4:01 PM
>…finite universe divide 10 by 3, in decimal, perfectly? >Even with 24 megabytes of memory, the "3" digits >would march on and there would be a loss of "perfection" somewhere. Exactly. (Though some number can be represented, it's still a matter of what we're talking about, and what `conversions' need to take place.) >It's a major breakthrough to realize that computers, despite their popular >reputation, are not always exact in the way they handle numbers. Yep, it's also a major breakthrough when folks realize (even though they know) that the fundamental builtin types in most languages have limits. >A good book on numeric methods will emphasize how the programmer needs to >take accuracy (and inaccuracy) into account during calculations. I couldn't agree with your more re this recommendation. (Ditto for that perhaps a better shot can be have using 'long' (in C), for instance and "doing it yourself".)
#48614From: matthew straneyOct 22, 1995 9:17 PM
Hi Jim, I have heard that NASA during the moon program use 3.14 as PI not 3.1415…, which is what I would expect. Though the instructor told us this while showing us how hard it is to be truely accurate.
#48615From: Jim ButterfieldOct 23, 1995 6:25 AM
> NASA used 3.14 instead of 3.1415927… I doubt it. Then again, I'm told that on one occasion in the 19th century, a state legislature passed a law that in that state, the value of pi would be taken as 3. Seems they wanted to simplify things .. 🙂 –Jim
#48631From: Peter WadeOct 24, 1995 5:06 PM
> Then again, I'm told that on one occasion in the 19th century, a state > legislature passed a law that in that state, the value of pi would be > taken as 3. Seems they wanted to simplify things .. 🙂 I read about this years ago in one of Isaac Asimov's essays. He said that someone had found a passage in the bible where they described something round in a temple and he worked out pi from that. Peter Wade Autopiloting from London, England
#48638From: Betty Clay/SYSOPOct 24, 1995 11:21 PM
The passage you mentioned is in II Kings – forget the chapter and verse – that was describing one of the fountains or baths in the court of Solomon's temple. It gave the circumference and the diameter, which had a ratio of three. The fountain was, as best I can remember without checking, about 42 feet in circumference, and was plated with pure gold. —Betty
#48639From: Jim ButterfieldOct 25, 1995 8:24 AM
It's I KINGS 7, verse 23: (quoting Revised Standard): "Then he made the molten sea: it was round, ten cubits from brim to brim, .. and a line of thirty cubits measured its circumference". Now, Betty, you can't expect a man who has seven hundred wives, princesses, and three hundred concubines to have TIME to measure the value of pi to more that one place! 🙂 –Jim
#48644From: Betty Clay/SYSOPOct 25, 1995 6:59 PM
I knew that you'd look it up and have it exactly, Jim. <g> Yep, he was pretty busy, all right!! —Betty
#48616From: Greg Comeau@Comeau CmptgOct 23, 1995 7:30 AM
> I have heard that NASA during the moon program use 3.14 as PI not 3.1415…, >which is what I would expect. If you mean they puposely did so, I'd question the context. If you mean some part of the code did so, I wouldn't doubt it. >Though the instructor told us this while showing >us how hard it is to be truely accurate. I think you mean that because the round off errors can build up as the expressions are more involved? True, but it's so easy to get those extra digits out of pi, why not just use them? I once knew it to 51 places (wonder if I still do), a couple (in Princeton U?) knows it to 1000 places, and besides, it can be calculated.
#48545From: Greg Comeau@Comeau CmptgOct 18, 1995 4:02 PM
>i tryed printf("%7.000000\n, number);…It dident work. BTW, all few posts, including mine earlier today, are assuming that you're still trying to output floats, and that the above was a typo somehow, and hence didn't comment on this, but actually, is it a typo when you posted your problem, or do you not have an 'f' in your printf % specification? That is, do you actually have "%7.00000\n", number? Or do you have somethig like "%7.0f\n", number?
#48585From: Douglas HornalOct 21, 1995 1:31 AM
It was a typo in my post…it was "%7.000000f\n",number -Daniel (useing my dads account)
#48460From: Dan NamayOct 11, 1995 5:02 PM
Doug, If you want to use a counter, it's best to use an integer instead of a float. Change to: int number . . . while (number >= 0) . (printf (%d\n", number); . This should work. As a matter of fact, I've got it running as I type this. (I have the SAS C compiler.) It DOES take a little time to get through the 1,000,000 iterations, though. The decimal comes from the "float" declaration. The imprecision comes from the round-off error in the computer's designation of floating point numbers. Others should be able to elaborate better than I. There is also a brief description of this _somewhere_ in your book; I remember reading it within the first five chapters. Hope this helps. Wolf Pup – Howlin' at the Net, Six-Packin' & Diggin'
#48535From: Douglas HornalOct 18, 1995 12:03 AM
I tred to do this, but with the aztek C compilar, int only goes +32,xxx to -32,xxx (I forget exactly how high and low) , but I'll try a diffrent datatype. -Daniel (useing my dads account.
#48539From: Dan NamayOct 18, 1995 11:24 AM
Dan, Instead of using "int" as your declaration, use "long". That will give you 4 Bytes which allows from -2,147,483,648 to +2,147,483,648. Aztec's "int" uses 2 bytes. It's "long" uses 4. Lattice/SAS uses 4 for both. Look at page 68 and 69 of you book. It explains it a little better. Scream if you need anything else. Unfortunately, though, once you get past the basics, I'm not going to be any help. Wolf Pup – Howlin' at the Net, Six-Packin' & Diggin'
#48546From: Greg Comeau@Comeau CmptgOct 18, 1995 4:02 PM
>Instead of using "int" as your declaration, use "long". That will give you >4 Bytes which allows from -2,147,483,648 to +2,147,483,648. He can also get a few more ounces out of the number (if it's positive) by, as Jim recommends, having it as an unsigned as well. Nonetheless, I think we need to see what the actual problem is, before presenting these solutions (though they _are_ good food for thought for sure).
#48547From: Dan NamayOct 18, 1995 4:36 PM
> He can also get a few more ounces out of the number (if it's positive) by, as Jim recommends, having it as an unsigned as well. Nonetheless, I think we need to see what the actual problem is, before presenting these solutions (though they _are_ good food for thought for sure). I agree – it's good to know the problem. I'm pretty new at this and was just trying to offer a simple solution to his original countdown problem. Since he only counted from 1,000,000 to 0, I didn't think he would need the extra available with unsigned. On anything more advanced, I will DEFINITELY wait for your, Jim's, or another more experienced programmer's advice 🙂 Wolf Pup – Howlin' at the Net, Six-Packin' & Diggin'
#48558From: Werner KazmierzakOct 19, 1995 5:05 PM
Dan, even with 4 Bytes, math will not be sufficiant for commercial programs, as accuracy is only granted for ? 21,474,836 $ plus 48 c. The summarized cash movements will exceed this value even with small companies. So you have to write your own at least 8 Byte math routines to exactly calculate the savings of Uncle Scrooge Duck (or Uncle Bill Gates) :-). – wkc – … via AP from Hamburg, Germany
#48560From: Dan NamayOct 19, 1995 5:43 PM
Werner, > even with 4 Bytes, math will not be sufficiant for commercial programs, as accuracy is only granted for ? 21,474,836 $ plus 48 c. Unfortunately, when it comes to "exact" math calculations in c, I'm not the one to ask. The 4 Bytes I was discussing were for the simple countdown routine that Dan Hornel needed. I purposely left the exact math part to someone who knows better. On these points, I stick with the easy ones. And I'll let Unca Bill Gates do his own programming to track his fortune. After all, he did such a Wonderful job with Win/Lose '95 :<) Wolf Pup – Howlin' at the Net, Six-Packin' & Diggin'
#48575From: Phillip WoollerOct 20, 1995 10:52 AM
I ended up writing string maths multiply and divide, I couldn't work out how to do 8 byte multiply/divide 🙁 Phill
#48586From: Werner KazmierzakOct 21, 1995 2:59 AM
Phillip, >> I couldn't work out how to do 8 byte multiply/divide well, if you got Knuth Vol. 2 `Seminumerical Algorithms', there is extensive discussion of this matter. And yes, while multibyte addition/subtraction is quite simple using the ADDX and SUBX commands, multiplication is not that easy, and division is the hardest thing to deal with. I wrote these routines some years ago. They are not the best way to do it, I'm sure, but if you are interested to have a look at this dirty code, I would like to mail it to you. – wkc – … via AP from Hamburg, Germany
#48662From: Phillip WoollerOct 27, 1995 4:33 AM
Ok, I would like to see the code Phill
#48591From: Jim ButterfieldOct 21, 1995 8:17 AM
Some folks love to wrestle with questions such as how to do a binary multiply/divide (that includes me .. I tend to write the code fresh rather than use macros). Other folks just want to get the job done. And a few trust their floating point processors (even that of the Pentium) to solve any problem that might arise (mistake!). Next time you run up against such a question, holler for help here. LOTS of members will jump in with their own (a) code, or (b) outline of how to go about it. –Jim
#48663From: Phillip WoollerOct 27, 1995 4:36 AM
Well, I have a string multiply in a system I wrote, it has to multiply a 9(4.6) by a 9(3.2) or a 9(3). How would you do this??? Phill
#48543From: Jim ButterfieldOct 18, 1995 12:58 PM
.. did you try UNSIGNED? –Jim