|
Post by Brandon Parker on Aug 16, 2020 21:03:08 GMT -5
Here is the link to the Rosetta Code page for calculating the value of e. Calculating the value of eHere's a simple method to calculate the value of e to eight decimal places. And, yes I know Liberty BASIC has "exp(x)" which returns e^x, but the task is to calculate e. Print calculateEulersNumber()
Function calculateEulersNumber() Epsilon = 1.0e-15 fact = 1 e = 2.0 n = 0
While (abs(e - e0) >= Epsilon) e0 = e n = (n + 1) fact = (fact * 2 * n * ((2 * n) + 1)) e = (e + ((2 * n) + 2)/ fact) Wend calculateEulersNumber = e End Function Feel free to upload more examples. {:0) Brandon Parker
|
|
|
Post by tsh73 on Aug 17, 2020 4:21:07 GMT -5
Actually 15. You just need to ask politely...
Print calculateEulersNumber() fmt$="#.###############" print "Series",using(fmt$, calculateEulersNumber()) print "LB EXP()", using(fmt$, exp(1))
2.71828183 Series 2.718281828459045 LB EXP() 2.718281828459045
|
|
|
Post by tsh73 on Aug 17, 2020 4:33:08 GMT -5
Basic sum \sum_n=0 1/n! Somehow last digit is 6 while exp gives 5.
fmt$= "#.###############" 'print len("###############") '15 'print using(fmt$, 10/3) 'after 15, prints garbage
'\sum_n=0 1/n!
f = 1 n = 0 while 1/f>=1e-15 if n then f=f*n e=e+1/f print n, 1/f, using(fmt$, e) n = n+1 wend print "----------------------" print "Series",using(fmt$, e) print "LB EXP()", using(fmt$, exp(1))
|
|
|
Post by Brandon Parker on Aug 17, 2020 13:47:03 GMT -5
Yeah, I had this other one as well, but you have to enter really large numbers of "n" to approximate "e" closely.
Function e(n) For i = 1 To n e = (1 + (1/ i))^i Next i End Function
I would have used Using() within the function, but LB seems to be truncating the value "NO MATTER WHAT" when returning it from the function call UNLESS the Using() function is encapsulating the function call. This seems like a bug to me...
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Aug 18, 2020 2:58:28 GMT -5
Please give an example
USING results in a string - string would not be truncated any further?
fmt$="#.###############"
print func1() '8 digits print using (fmt$,func1()) '15 digits print func2$() 'USING in a function, returns string, 15 digits
end function func1() func1 = 1/3 end function
function func2$() fmt$="#.###############" func2$=using (fmt$,1/3) end function
|
|
|
Post by tsh73 on Aug 18, 2020 7:46:07 GMT -5
Arbitrary number of digits (checked for first 50)
Exact (off the net) e=2.7182818284590452353602874713526624977572470936999... Calculated e=2.7182818284590452353602874713526624977572470936999
'spigot algorithm print "Exact (off the net)" print "e=2.7182818284590452353602874713526624977572470936999..." print "Calculated" print "e=2."; N=50
dim a(N+1) a(0)=2 for i = 1 to N+1 a(i)=1 next
for k = 1 to N-1 for i = 1 to N+1 a(i)=a(i)*10 next
for i = N+1 to 1 step -1 d=int(a(i)/(i+1)) r = a(i) mod (i+1) a(i)=r a(i-1)=a(i-1)+d
next print d;
next
|
|
|
Post by Brandon Parker on Aug 18, 2020 10:43:13 GMT -5
USING results in a string - string would not be truncated any further? It's actually not the function return that truncates the value that causes the truncation. So, unless you change the function to return a string or you use Using() to encapsulate the function return LB will truncate it. This shows the truncation ... Print calculateEulersNumber()
Function calculateEulersNumber() Epsilon = 1.0e-15 fact = 1 e = 2.0 n = 0
While (abs(e - e0) >= Epsilon) e0 = e n = (n + 1) fact = (fact * 2 * n * ((2 * n) + 1)) e = (e + ((2 * n) + 2)/ fact) Wend calculateEulersNumber$ = Using("#.###############", e) calculateEulersNumber = Val(calculateEulersNumber$) End Function And, if you do not believe me just run this code. This is as simple as it gets... Print test()
Function test() test = Val("1.234567891234567") End Function To me, it looks like Val() is not really supporting double precision. If you remove the decimal point and make it an integer the entire number is returned correctly. {:0) Brandon Parker
|
|
|
Post by tsh73 on Aug 18, 2020 15:01:07 GMT -5
No test() returns LB number, that is double - produced with VAL or not If you print it with PRINT you only see 7(or 8) digits But if you print it with PRINT USING you can get all 15 digits - they were there all the time
fmt$="#.################"
'using a function Print test() Print using (fmt$, test()) print
'storing in a variable x = test() Print x Print using (fmt$, x)
Function test() test = Val("1.234567891234567") End Function
1.23456789 1.2345678912345670
1.23456789 1.2345678912345670
|
|
|
Post by Brandon Parker on Aug 18, 2020 22:04:56 GMT -5
Right...it should not be truncating the value even if you can get it with the Using() function or not. Using() is slated for formatting numbers not for ensuring "everything" available is returned.
The actual value as it is stored in LB's actual runtime calculations should be returned all the time (i.e. all of those digits) unless Using() is invoked to remove some. By doing what LB is doing in this scenario results could be outside of what is expected.
For the result ...
This is Single-Precision Floating-Point [/quote]
This is Double-Precision Floating-Point
Since LB has Double Precision Floating Point one does NOT expect to get Single Precision Floating Point out of an operation that involves one or more Double-Precision values normally in Computer Science.
This code below shows automatically evaluate to the Double Precision value since it involves one or more of them from the start of the calculation, but it does not without forcing it to with the Using() function. In my mind, this is a mistake.
Print (1.234567891234567 + 0.000000001)
Somewhere in the docs, it states this, but I can't remember where. I pulled this from the online help which is old...
There is an option in the Debugger to show unrounded numbers (right-click in the variable window), but if one is not aware of this and either looks at a printed, file output, or normal Debugger then things look off. One has to "know" that one has to use the Using() function when printing/outputting to file to get the Double-Precision Floating-Point version; otherwise, the Single-Precision Floating-Point value is used which is just wrong when Double-Precision Floating-Point values are explicitly used/calculated by the program.
Either way, we digress. This would be an issue for Carl to resolve in LB 5 going forward if it exists there too. I have not checked...
{:0)
Brandon Parker
|
|
|
Post by Rod on Aug 19, 2020 6:07:41 GMT -5
If I recall the debate it was really all about PRINT not really anything to do with the background maths. It seemed sensible to truncate float values for display. We know that if we want to display the full float we need USING()
The alternative is reams and reams of number display that few folks can interpret, it gets a bit meaningless beyond four or more digits. I quite like the truncation. But I do know the full float value is used in the background and I do know I can display it if really needed.
|
|
|
Post by Brandon Parker on Aug 19, 2020 8:46:39 GMT -5
Yeah, and it is a matter of debate which I why I said we were digressing from the initial thread's objective. Personally and in my CS experience, it would be better to always deal with the full value and truncate as needed not the other way around.
Unless there's further discussion about actually calculating the value of Euler's Number then I think we can leave it at that.
{:0)
Brandon Parker
|
|