Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: VB6 Slight Math Problem?  (Read 1532 times)

Azkanan

  • Bay Watcher
    • View Profile
VB6 Slight Math Problem?
« on: March 13, 2012, 03:28:10 pm »

Theory

The idea is that in the image below that all buttons are relative to each other by either the same, plus or minus 1 from its neighbour.

The way it currently is being generated (Messy, I know), that it checks each button from left to right, then moving to the next line.

A B C
D E F
G H I

A = Rand 1-5 (This is the initiator)
B = A + (Random: -1, 0 or +1)
C = B + (Random: -1, 0 or +1)
D = A + (Random: -1, 0 or +1)

E = (B + D) / 2 (Then "Fix" cuts off any decimals)
F = (C + E) / 2

Etc.


The Problem



The Code

Code: [Select]
Dim VarChange As Integer
Dim VarTemp As Integer
Dim VarTemp2 As Integer

VarChange = 0
VarTemp = 0
VarTemp2 = 0

Command1.Caption = Rand(1, 5)
VarChange = 0
VarChange = Rand(-1, 1)
Command2.Caption = Command1.Caption + VarChange
VarChange = 0
VarChange = Rand(-1, 1)
Command3.Caption = Command2.Caption + VarChange
VarChange = 0
VarChange = Rand(-1, 1)
Command4.Caption = Command1.Caption + VarChange

VarChange = 0
VarChange = Rand(-1, 1)
Command5.Caption = Command4.Caption + VarChange
VarTemp = 0
VarTemp = (Command4.Caption + Command2.Caption) / 2
VarTemp2 = 0
VarTemp2 = Rand(-1, 1)
Command5.Caption = VarTemp + VarTemp2
Command5.Caption = Fix(Command5.Caption)

VarChange = 0
VarChange = Rand(-1, 1)
Command6.Caption = Command5.Caption + VarChange
VarTemp = 0
VarTemp = (Command5.Caption + Command3.Caption) / 2
VarTemp2 = 0
VarTemp2 = Rand(-1, 1)
Command6.Caption = VarTemp + VarTemp2
Command6.Caption = Fix(Command6.Caption)

VarChange = 0
VarChange = Rand(-1, 1)
Command7.Caption = Command4.Caption + VarChange

VarChange = 0
VarChange = Rand(-1, 1)
Command8.Caption = Command7.Caption + VarChange
VarTemp = 0
VarTemp = (Command7.Caption + Command5.Caption) / 2
VarTemp2 = 0
VarTemp2 = Rand(-1, 1)
Command8.Caption = VarTemp + VarTemp2
Command8.Caption = Fix(Command8.Caption)

VarChange = 0
VarChange = Rand(-1, 1)
Command9.Caption = Command8.Caption + VarChange
VarTemp = 0
VarTemp = (Command8.Caption + Command6.Caption) / 2
VarTemp2 = 0
VarTemp2 = Rand(-1, 1)
Command9.Caption = VarTemp + VarTemp2
Command9.Caption = Fix(Command9.Caption)
« Last Edit: March 13, 2012, 03:41:09 pm by Azkanan »
Logged
A pool of Dwarven Ale.
WHO IS RESPONSIBLE FOR THIS ?

Azkanan

  • Bay Watcher
    • View Profile
Re: VB6 Slight Math Problem?
« Reply #1 on: March 13, 2012, 03:38:30 pm »

Addendum: I just updated the code; However, I am sometimes getting an overflow error on line;

VarTemp = (Command8.Caption + Command6.Caption) / 2

How the hell does that cause an overflow? (3 + 4) / 2 = 3.5
« Last Edit: March 13, 2012, 03:41:23 pm by Azkanan »
Logged
A pool of Dwarven Ale.
WHO IS RESPONSIBLE FOR THIS ?

SP2

  • Bay Watcher
    • View Profile
Re: VB6 Slight Math Problem?
« Reply #2 on: March 13, 2012, 03:43:17 pm »

Is .Caption stored as a string?

And isn't 3.5 a float, not an integer?
Logged

Azkanan

  • Bay Watcher
    • View Profile
Re: VB6 Slight Math Problem?
« Reply #3 on: March 13, 2012, 03:54:01 pm »

Is .Caption stored as a string?

And isn't 3.5 a float, not an integer?

It seems to be adapting the captions as integers.

And the decimals still show up in integers, ergo, I had to Fix them.

*shrug*

The main problem seems to be my equation for averaging;
If I write it as A = B + C /2, then it doesn't work.
If I write it as A = (B + C) / 2, then it doesn't work.

Ideas?


A = B + C /2 Result:


3 = B
5 = C
A = 5.5

According to this, 3 + 5 / 2 = 5.5



A = (B + C) / 2 apparently works fine.




Edit;

So I just changed the above simulation to read from the labels' captions, rather than the generated numbers, and then the result went haywire. Looks like the error seems to be reading-from-caption related. :|
« Last Edit: March 13, 2012, 04:02:19 pm by Azkanan »
Logged
A pool of Dwarven Ale.
WHO IS RESPONSIBLE FOR THIS ?

kaenneth

  • Bay Watcher
  • Catching fish
    • View Profile
    • Terrible Web Site
Re: VB6 Slight Math Problem?
« Reply #4 on: March 13, 2012, 04:05:01 pm »

Yeah, Multiplation and Division take priority of Addition/subtraction, so:

3 + 5 / 2 = 3 + 2.5 = 5.5

However, a general solution to averaging two integers to give a correct integer average is hard; and depends on your rounding rules.

A = (B+C)/2 has potential overflow errors if the sum of B and C excess the integer limit (pretty rare)

A = B/2+C/2 will not crash, however, the average of 3 and 3 is 2 with this method.
 
Logged
Quote from: Karnewarrior
Jeeze. Any time I want to be sigged I may as well just post in this thread.
Quote from: Darvi
That is an application of trigonometry that never occurred to me.
Quote from: PTTG??
I'm getting cake.
Don't tell anyone that you can see their shadows. If they hear you telling anyone, if you let them know that you know of them, they will get you.

Azkanan

  • Bay Watcher
    • View Profile
Re: VB6 Slight Math Problem?
« Reply #5 on: March 13, 2012, 04:09:24 pm »

Thanks for the help, but it was just a problem caused by my laziness.



Code: [Select]
Dim VarChange As Integer
Dim VarTemp As Integer
Dim VarTemp2 As Integer

Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num8 As Integer
Dim num9 As Integer

    num1 = Rand(1, 10)
VarChange = Rand(-1, 1)
    num2 = num1 + VarChange
VarChange = Rand(-1, 1)
    num3 = num2 + VarChange

VarChange = Rand(-1, 1)
    num4 = num1 + VarChange

VarChange = Rand(-1, 1)
VarTemp = (num2 + num4) / 2
    num5 = VarTemp + VarChange
VarChange = Rand(-1, 1)
VarTemp = (num3 + num5) / 2
    num6 = VarTemp + VarChange

VarChange = Rand(-1, 1)
    num7 = num4 + VarChange

VarChange = Rand(-1, 1)
VarTemp = (num5 + num7) / 2
    num8 = VarTemp + VarChange
VarChange = Rand(-1, 1)
VarTemp = (num6 + num8) / 2
    num9 = VarTemp + VarChange

Command1.Caption = num1
Command2.Caption = num2
Command3.Caption = num3
Command4.Caption = num4
Command5.Caption = num5
Command6.Caption = num6
Command7.Caption = num7
Command8.Caption = num8
Command9.Caption = num9

Now I just need to tidy the code into Types and automate the process, so that I can generate a large image. /o\
Logged
A pool of Dwarven Ale.
WHO IS RESPONSIBLE FOR THIS ?

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: VB6 Slight Math Problem?
« Reply #6 on: March 13, 2012, 04:16:39 pm »

Problem detected: Tiles 6 and 9 are invalid.
Reason: With a 1 to the left and a 3 at the top, 2 is the only possible number that still fits in there. You can't add a random number in this case.
Logged