|
Post by tsh73 on Apr 3, 2018 3:18:14 GMT -5
Happened today, in VB.net Student tried to program Taylor series for sin(x) ( from Wikipedia, ) So he wrote f = x - x ^ 3 / 3.0! + x ^ 5 / 5.0! - x ^ 7 / 7.0! It compile... and run... *facepalm* (to be fair, we all happen to be there on some stage)
|
|
|
Post by hooshnik on Apr 3, 2018 18:18:28 GMT -5
Is this something specific to VB? I know something is wrong but I can't spot the error.
|
|
cundo
Full Member
Muchas Gracias!!
Posts: 146
|
Post by cundo on Apr 3, 2018 20:44:54 GMT -5
Factorials? had to do a search. I suppose this build a table depending on x value. I'm 'rusty' in math sorry
|
|
|
Post by tsh73 on Apr 4, 2018 0:48:27 GMT -5
Yes, that suposed to use factorial for 3!, 5! etc But in MS BASIC, "!" is type-character - it says that variable (end even constant) is single-precision floating point number. So it ends up being valid code, and compiles.
And calculates wrong results of cource.
|
|
GWS
New Member
Posts: 18
|
Post by GWS on Apr 4, 2018 13:36:49 GMT -5
I was always being told off by mathematician colleagues, that my scribbling describing some math problem wasn't written as proper maths equations. Of course not - you can't program a computer to understand equations - only language expressions. There's often a difference. Anyway, I'm very pleased to have got my first bit of LB code working .. In the example we're discussing, the poor student needed a Function to calculate the factorial of a number. (So for example 3! will be 3*2*1 = 6) Here's my test code .. ' test the Factorial function x = fact(3) print x ' 3! (factorial 3 = 3 * 2 * 1 = 6)
pi = 4*atn(1) ' this equation saves remembering all the digits in pi
v = pi/6 ' pi radians = 180 degrees, so 30 degrees = pi / 6
' calculate the sine of 30 degrees s = v - v^3 / fact(3) + v^5 / fact(5) - v^7 / fact(7)
print s
end
Function fact(value) ' calculate the Factorial of the given value n = value result = n
n = n - 1
while n > 1 result = result * n n = n - 1 wend
if result = 0 then result = 1 ' 0! is by definition 1 end if
fact = result
End Function
This seems to work pretty well. The test calculates the sine of 30 degrees. This should be exactly 0.5 - but with computers, decimal values are hardly ever precise. So we get 0.49999999 - near enough .. Interesting example .. all the best, Graham
|
|
|
Post by hooshnik on Apr 4, 2018 14:41:16 GMT -5
Yes, that suposed to use factorial for 3!, 5! etc But in MS BASIC, "!" is type-character I forgot! I only remembered % - integer. Brutal.
|
|
GWS
New Member
Posts: 18
|
Post by GWS on Apr 5, 2018 8:01:27 GMT -5
I've just tried the example for sin(30) using three extra terms, and Mask$ to show 20 decimal places - Wow ! LB's calculations are smooth and very accurate. It gives me a lot of confidence in using LB's maths facilities .. ' test the Factorial function x = fact(3) print x ' 3! (factorial 3 = 3 * 2 * 1 = 6)
pi = 4*atn(1) ' this equation saves remembering all the digits in pi
v = pi/6 ' pi radians = 180 degrees, so 30 degrees = pi / 6
' calculate the sine of 30 degrees s = v - v^3/fact(3) + v^5/fact(5) - v^7/fact(7) + v^9/fact(9) - v^11/fact(11) + v^13/fact(13)
's = sin(v)
mask$="#."
for i=1 to 20: mask$ = mask$ + "#": next '20 digits after "."
print using(mask$, s)
print s
end
Function fact(value) ' calculate the Factorial of the given value n = value result = n
n = n - 1
while n > 1 result = result * n n = n - 1 wend
if result = 0 then result = 1 ' 0! is by definition 1 end if
fact = result
End Function
best wishes, Graham
|
|
|
Post by Carl Gundel on Apr 5, 2018 9:03:32 GMT -5
I've just tried the example for sin(30) using three extra terms, and Mask$ to show 20 decimal places - Wow ! LB's calculations are smooth and very accurate. It gives me a lot of confidence in using LB's maths facilities .. ' test the Factorial function 'code removed from this quoted post
best wishes, Graham Liberty BASIC just uses the computer's math coprocessor for trig calculations (and logs), so don't think that you are getting 20 digits of correct precision even if you force it to show you 20. Here is a link to the IEEE double precision floating point spec which says that you get "from 15 to 17 significant decimal digits precision". To me that means 15 digits, but the problem of course is that you cannot represent floating point values perfectly with this model, so there is always some lack of accuracy in representation. This of course becomes the subject of endless debate and discussion. en.wikipedia.org/wiki/Double-precision_floating-point_format
|
|
|
Post by Carl Gundel on Apr 5, 2018 9:07:22 GMT -5
In the past people have made bug reports complaining about small errors in floating point values, say for example a float that displays as 3.249999 and the claim is that it should be 3.25 and so Liberty BASIC has a bug. Then I would run the same code on QBASIC and the result would be exactly the same 3.249999. There is no bug. IEEE floating point cannot represent all values in the space of real numbers, so this happens.
|
|
GWS
New Member
Posts: 18
|
Post by GWS on Apr 5, 2018 12:34:30 GMT -5
I'm grateful for your comments Carl. 15 significant figures is just a tad better than the 4 figures we used to work with in slide rule and log table days ..
When I said I'm pleased with the smoothness of LB's calculations, I meant more than the precision ..
It is quite happy with mixed integers and floats - no required definition of assorted variable types, and a sweet scientific '1e-12' format if needed.
LB just gets on with the calculations with no fuss and the best possible accuracy - great stuff.
all the best,
Graham
|
|
|
Post by Carl Gundel on Apr 5, 2018 12:54:05 GMT -5
I'm grateful for your comments Carl. 15 significant figures is just a tad better than the 4 figures we used to work with in slide rule and log table days .. When I said I'm pleased with the smoothness of LB's calculations, I meant more than the precision .. It is quite happy with mixed integers and floats - no required definition of assorted variable types, and a sweet scientific '1e-12' format if needed. LB just gets on with the calculations with no fuss and the best possible accuracy - great stuff. all the best, Graham I'm glad you like it. I'm sure that some BASIC programmers would like to have integer variable types (for example myVar%) for performance reasons, but this wouldn't actually make Liberty BASIC run noticeably faster.
|
|