QB64 Phoenix Edition
Incorrect result - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Incorrect result (/showthread.php?tid=2940)

Pages: 1 2


Incorrect result - Chris - 08-14-2024

Hello.
Please correct the code. It gives an incorrect result.

P# = 3.141592653589793#
K# = 57.29577951308232#
S# = 279.559511
H# = 40
CLS: N# = 1: F# = 1: GOTO 22
21 F# = F# + N#
22 T# = SQR(2) * SQR(S# / (P# * F# / 180 - SIN(F# / K#))): L# = 2* T# * SIN(F# / 2 / K#)
IF (L# - H#) < 1E-10 THEN 23
IF H# > L#  THEN 24
GOTO 21
23 PRINT USING "##.####"; T#: GOTO 25
24 F# = F# - N#: N# = N# / 2: GOTO 21
25 PRINT " Ok."


T# = 25.0000 -> result correct
T# = 24.7711 -> incorrect result

Chris


RE: Incorrect result - SMcNeill - 08-14-2024

I have no idea what's wrong with it, as it's mainly just a math formula, but here it is without all the GOTO and spaghetti code, if that helps anyone else sort out what it might possibly be doing wrong:

Code: (Select All)
P# = 3.141592653589793#
K# = 57.29577951308232#
S# = 279.559511
H# = 40
Cls: N# = 1: F# = 1
Do
T# = Sqr(2#) * Sqr(S# / (P# * F# / 180# - Sin(F# / K#))): L# = 2 * T# * Sin(F# / 2 / K#)
If (L# - H#) < 1D-10 Then Print Using "##.####"; T#: Exit Do
If H# > L# Then F# = F# - N#: N# = N# / 2
F# = F# + N#
Loop
Print " Ok."



RE: Incorrect result - luke - 08-14-2024

This line is wrong:
Code: (Select All)
IF (L# - H#) < 1E-10 THEN 23
it should be
Code: (Select All)
IF ABS(L# - H#) < 1E-10 THEN 23


While we're at is, here's a version that's not so obtusely written:
Code: (Select All)
DefDbl A-Z
S = 279.559511
H = 40
N = _D2R(1)
F = _D2R(1)
EPSILON = 1D-10

U = Sqr(S * 2)
Do
    T = U / Sqr(F - Sin(F))
    L = 2 * T * Sin(F / 2)
    If Abs(L - H) < EPSILON Then Print Using "##.####"; T: Exit Do
    If L > H Then
        F = F + N
    Else
        N = N / 2
        F = F - N
    End If
Loop



RE: Incorrect result - Chris - 08-14-2024

Great, great, great.
Thank You very much Luke. That I didn't think of adding "ABS" either. Now it counts properly.
 Best regards - Chris.


RE: Incorrect result - Pete - 08-14-2024

Oh come on Steve, everyone knows when it comes to providing A BS solution, you're the best!

Pete Big Grin


RE: Incorrect result - SMcNeill - 08-14-2024

(08-14-2024, 03:03 PM)Pete Wrote: Oh come on Steve, everyone knows when it comes to providing A BS solution, you're the best!

Pete Big Grin

I was just going to change the PRINT statement to print 25.0000, but Luke found a different solution first.  

I still think mine's better though.  Chris never specified HOW to fix it.  He just needed 25 as the correct result.


RE: Incorrect result - Pete - 08-14-2024

Big Grin Big Grin Big Grin


RE: Incorrect result - Chris - 08-15-2024

Without creating a new topic.
INPUT A#
PRINT A#
When entering data:
78.53 displays 78.53
78.54 displays 78.54000000000001
78.55 displays 78.55
What's going on here.

Chris


RE: Incorrect result - TerryRitchie - 08-15-2024

(08-15-2024, 06:29 PM)Chris Wrote: Without creating a new topic.
INPUT A#
PRINT A#
When entering data:
78.53 displays 78.53
78.54 displays 78.54000000000001
78.55 displays 78.55
What's going on here.

Chris
Floating point anomalies that are common in all programming languages after reading up on the subject. I just recently posted a question concerning this same thing dealing with a different topic.


RE: Incorrect result - Kernelpanic - 08-15-2024

Luke is a spoilsport!
goto ... 20 ... goto 15 ... goto 25 ... goto 7 ... and so on

For a real programmer that's better than sex.  Tongue