Re: Number of decimals
"floobydoo2000" <[email protected]>
| Newsgroups | gmane.comp.windows.gui4cli |
|---|---|
| Message-ID | <[email protected]> |
Thanks to both Frans and Fred for helping me understand how G4C handles numbers. When I discovered that the math routines are good for 15 or more significant digits after I declared it was 6, my face turned so red that I broke out in a rash. I'd like to put what's happening in my own words even though Fred already said it, just to be sure. When you do a calculation like $($x*45), everything between the outside brackets goes to the math routine which does all calculations in 15 digit or better precision. It is only when the math routine is finished that rounding occurs. This happens because the number is converted into a string and a decision during development had to be made regarding how many decimal places to hold. 6 decimal places was chosen. If I'm correct, then Frans could use the math routine in G4C without resorting to dll's if he really wants to. I'm thinking of something like the following (Note I did NOT DEBUG this, it is just an idea to try) a=10^-8 b=20 c=$($a*$b) c will be zero, which would be similar to the case Frans presentd, so instead: e=1000000 c=$($e*$a*$b) //this way c retains enough significant digits //so now when you calc with c you need to make the appropriate adjustment using e. So for example, if the next calc using c is: c^2+a*c^.33 then you would instead do (e* ((c/e)^2 +a*(c/e)^.33) ) You wouldn't multiply the result above by e unless you needed it in a further calculation. This is starting to look a little clunky, personally, I'm hoping either for a math dll or for the 6 decimal place conversion to string to be changed to 15. The latter sounds the easiest, unfortunately, I cannot do either approach. C compilers give me a gut ache. Thanks Mike --- In [email protected], "fredfjord@..." <fredfjord@...> wrote: > > Hi Frans, > > //Is there a 'trick' to convert a caclulation immediately to a string? > //Cannot use E = $(5/10000000) > > Easy, just use E = (5/10000000) > Since you use it later in a calculation then at that time it will be calculated and not now and rounded. > > Fred. > > > --- In [email protected], "Marianne" <marianne976@> wrote: > > > > Hi Mike and Fred > > > > Me again and thanks for all the info and also on the ttmath and bc programs. > > > > Mike in your posting you said the following and I quote: > > > > "I'd like to see exactly how you got this because it is very curious." > > > > Here is a small part of the original program, the problem part. The other values is from calculations from the program. I hope it will help (The comments were just for me while I was struggling): > > > > Run the program with E = 0.0000005, your answer will be app 0.25, this is what it must be. > > > > Change E to 0.000001 and run again. The answer is now 0.15003 > > > > Gcwin open > > A8 = 362880 > > A5 = 0.066167 > > M2 = 19 > > C4 = 0.95206 > > //E must be a string else it is rounded to 6 - this is my problem. > > //Is there a 'trick' to convert a caclulation immediately to a string? > > E = "0.0000005" //Cannot use E = $(5/10000000) or E = 0.0000005, becomes 0.000001 > > // > > Z=$(6+($M2-2)/2) > > P5=$((PI*2/$Z)^(1/2)*($Z)^($Z)) > > P5 = $($P5 * $e) > > Z=$($Z-5) > > zz = $($Z*($Z+1)*($Z+2)*($Z+3)*($Z+4)) > > B2=$($P5/$zz) > > A5=$($A5*$A8/$B2); B3=$A5 > > F=$(1-$C4+$B3) > > say "Answer is $F" > > > > This is what I am talking about. (The large values are the problem, will always be the case. In the final answer I am only interested in the first 4 decimals.) > > > > Hope it is clear. > > > > Fred I will look again at the dll. As said I already wrote one and is busy testing it. > > > > Once again thanks for everything. > > > > Kind regards. > > > > Frans > > > > > > > > > > > > > > --- In [email protected], "fredfjord@" <fredfjord@> wrote: > > > > > > Hi, > > > > > > The evaluation in G4C uses the double data type and gives about 15 digits precision. > > > A calculation should be accurate only the result is rounded because it is converted into a string with standard 6 digit precision. > > > > > > When you do a calculation, store it in a var (then rounded) and uses the var it in a next calculation the result could be a little less accurate then when in one calculation. > > > > > > My intention was to use bigger precision or other notation as string result. > > > Also using a double result in a hex string format for later calculation would be possible. > > > Like a said a set float command would be better but i suppose Dimitris is busy (well, we all are :)). > > > > > > @Frans: > > > I just looked at FreeBASIC and it looks good but i have not made a dll with it yet. > > > Perhaps i can make your functions in the eval module or i could make your dll into a G4C dll. > > > In the Sql3 source you can see what you need for a G4C dll, the TRex module is even smaller, only a few functions. > > > > > > I agree to use what is available in G4C but Dimitris has it made easy to use a dll in G4C. > > > And Windows is a big collection of dll's :) > > > > > > And we all like Gui4Cli else we would not be on this list :) > > > > > > Regards, > > > Fred. > > > > > > > > > --- In [email protected], "floobydoo2000" <michael.bernas@> wrote: > > > > > > > > I would agree that an answer of .15 when .24 is expected is inaccurate. With 6 signifcant digit math you should get 4 or 5 digit accuracy. Here, you didn't even get one. I'd like to see exactly how you got this because it is very curious. > > > > > > > > I'd also like to show that G4C holds small numbers like 0.000000005 accurately even if they get printed out as zero. the following code shows this: > > > > > > > > say $(0.000000005) > > > > say $(0.000000005/0.0000000025) > > > > > > > > results in: > > > > > > > > 0 > > > > 2 > > > > > > > > I do not view showing such a small number as zero to be a bug, but it would be a welcome feature to have such small numbers printed in scientific notation instead. > > > > > > > > I'd like to point out that using the math module that GUI4Cli in a math dll will not improve things as Fred suggests below. This module is really only good for 6 sigificant digits. I would recommend trying ttmath (google it, it's on sourceforge now, the original site www.ttmath.org seems to be gone) for anyone wanting to try it. The author demonstrated this module by writing a calculator called ttcalc. ttmath is, like bc, arbitrary precision. > > > > > > > > Thanks, > > > > Mike > > > > > > > > > > > > --- In [email protected], "Marianne" <marianne976@> wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Fred and Daz > > > > > > > > > > Once again thanks for all the information. > > > > > > > > > > This might sound strange but I am not that concerned about precision. I am more interested about the number of decimals shown. > > > > > > > > > > I wrote a program in Gui4Cli to calculate the probabilities for the F-probability distribution, say for an F-value of 1.49 with Df1 = 3 and Df2 = 19. The answer should be 0.25 > > > > > > > > > > At one stage I perform a calculation with a variable with 7 decimals, something like 0.0000005.... This value is given as 0.000001 by G4Cli (rounded to 6 decimals. My final answer is 0.15??? using Gc4 instead of 0.24???? > > > > > > > > > > What I did then was to define the variable say A directly in Gui4cli as A = 0.0000005 and repeat the calculation and I got the correct answer.(I just want to point out that I know that is not a problem of Gui4Cli.) > > > > > > > > > > Fred, last night I wrote a dll using FreeBasic, testing it and it works fine. Unfortunately it is a system dll and not a "Gui4Cli" dll so one has to use the sysdll command. (I do not have a clue how to write a Gui4Cli dll, although I had a look at the dll tutorial.) > > > > > > > > > > This dll will be app. 20 KB and includes the Normal, T-, F- Chi-Square, Gamma and other distributions also with the inverse functions, but I still prefer not to use dlls unless it is not possible to perform all calculations within Gui4Cli (I repeat, I like Gui4Cli.) > > > > > > > > > > The majority of the above distributions work in Gui4Cli, except for the rounding in a few cases. > > > > > > > > > > Once again I want to thank all of you for the replies. > > > > > > > > > > Regards. > > > > > > > > > > Frans > > > > > > > > > > > > > > > --- In [email protected], "fredfjord@" <fredfjord@> wrote: > > > > > > > > > > > > Hi, > > > > > > > > > > > > The expression evaluation uses 'sprintf (out, "%f", res))' at the end and the default precision is 6 digits for %f. > > > > > > I think the best solution would be to add a SET FLOAT witdh.precision statement by Dimitris with default .6 to keep compability. > > > > > > > > > > > > Another solution would be to make a math dll, i can try to use the same module G4C uses for math in a dll with a greather precision. > > > > > > It can take a while since i have a cold and yesterday had 39,3 degrees so i don't think very fast :) > > > > > > I already did make the eval module as a command line tool so it should not be to difficult. > > > > > > > > > > > > Using bc you would have a high precision calculator as long as you use only strings and no calculation in G4C. > > > > > > > > > > > > Regards, > > > > > > Fred. > > > > > > > > > > > > > > > > > > > > > > > > --- In [email protected], dazroo <daz4roo@> wrote: > > > > > > > > > > > > > > Apologies to group for one of those "scattered" replies. > > > > > > > > > > > > > > On 18/09/10 00:02, Marianne wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Hi Daz and Mike > > > > > > > > > > > > > > > > Thanks for the response but BC will not help me. I apologise if my > > > > > > > > knowledge is not up to date or standard. > > > > > > > > > > > > > > > > I am using Gui4Cli currently for all calculations as I can perform all > > > > > > > > calculations in Gui4Cli. > > > > > > > > > > > > > > Floating Point Arithmetic (FPA) is notoriously error-prone in many computer > > > > > > > implementations. Here's one explanation: > > > > > > > http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems > > > > > > > > > > > > > > > > > > > > > > Secondly I am a self studied programmer and do > > > > > > > > not know a lot about programming. I only know a little Basic. Here is an > > > > > > > > example and if I am off target please let me know. > > > > > > > > > > > > > > > > Take the following example: > > > > > > > > A = 1 > > > > > > > > B = 1.538 > > > > > > > > > > > > > > > > So A/B = 0.649772579597141...... and I want to round this off say to 8 > > > > > > > > decimals. I cannot do the following: > > > > > > > > > > > > > > That result is already inaccurate (before rounding) :( > > > > > > > > > > > > > > Copy/Paste the following into the bottom box at: > > > > > > > http://sciencesoft.at/bc/?lang=en > > > > > > > -------------------------------------------- > > > > > > > a = 1 > > > > > > > b = 1.538 > > > > > > > g4cans = 0.649772579597141 > > > > > > > a/b > > > > > > > a/b - g4cans > > > > > > > -------------------------------------------- > > > > > > > and click "Calculate" > > > > > > > > > > > > > > Result: > > > > > > > .65019505851755526657 .00042247892041426657 > > > > > > > > > > > > > > That's a g4c inaccuracy of .00042247892041426657 on that one > > > > > > > calculation. > > > > > > > > > > > > > > ( I get the answer of around .65019506 in three other programs. ) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > C = $($A/$B) = 0.649773 and send C to the external program, say BC, > > > > > > > > because Gui4Cli already rounded $C to 6 decimals. I can probably send > > > > > > > > $A/$B to BC (not tested yet as calculations are sometimes lenghty in > > > > > > > > statisics and not that easy to do it this way, if possible. (Will not be > > > > > > > > possible if one uses loops for example.) > > > > > > > > > > > > > > > > > > > > > > I would recommend staying away from the in-built FPA and, instead, > > > > > > > call an external program (designed for the task) via [DOS.DLL] passing > > > > > > > FP-like numbers as strings. > > > > > > > Once your FP calculations are done and rounded, then you can > > > > > > > present the results in your gui as strings in the precision required. > > > > > > > > > > > > > > > In other words it seems to me that all calculations therefore have to be > > > > > > > > performed with an external program if you want rounding off to more than > > > > > > > > 6 decimals. Although Gui stands for Graphical User Interface that is not > > > > > > > > what I want. (The majority of other languages do have a Gui interface, > > > > > > > > but I like Gui4Cli because of its simplicity. > > > > > > > > > > > > > > FP = major complexity :) - no fault of Gui4Cli. > > > > > > > Use Gui4Cli with a reliably accurate external library routines for FPA. > > > > > > > > > > > > > > > > > > > > > > > Why do I need so many decimals? This is to perform for example > > > > > > > > convergence problems of Functions, like probabilities and one will be > > > > > > > > suprised how quick Gui4Cli is.(My standards) > > > > > > > > > > > > > > > > If Gui4Cli can give decimals to more than 6 or if a new command such as > > > > > > > > 'Set Decimals to $value' can be added it will be great but I do not know > > > > > > > > how easy or difficult it will be. > > > > > > > > > > > > > > It would be a daunting task, I think. > > > > > > > Take your example which I gave to BC: > > > > > > > What value would more decimal places on the end of an already > > > > > > > inaccurate result be? ;) > > > > > > > > > > > > > > In other scripting language forums, I've seen strong > > > > > > > recommendations for avoiding doing currency calculations > > > > > > > in FPA (e.g. 7.55eur). Instead, they suggest do all calculations > > > > > > > in cents (755c) because you can't get unexpected rounding > > > > > > > anomalies using integer arithmetic (only programming bugs). > > > > > > > > > > > > > > > > > > > > > > > Regards > > > > > > > > > > > > > > > > Frans > > > > > > > > > > > > > > > > > > > > > > To you, also. > > > > > > > > > > > > > > daz > > > > > > > > > > > > > > > > > > > > > > > > > > > > ------------------------------------ Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/gui4cli/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/gui4cli/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/