Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect result
#1
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
Reply
#2
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."
Reply
#3
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
Reply
#4
Great, great, great.
Thank You very much Luke. That I didn't think of adding "ABS" either. Now it counts properly.
 Best regards - Chris.
Reply
#5
Oh come on Steve, everyone knows when it comes to providing A BS solution, you're the best!

Pete Big Grin
Shoot first and shoot people who ask questions, later.
Reply
#6
(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.
Reply
#7
Big Grin Big Grin Big Grin
Shoot first and shoot people who ask questions, later.
Reply
#8
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
Reply
#9
(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.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#10
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
Reply




Users browsing this thread: 3 Guest(s)