Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding to negative numbers 1

Status
Not open for further replies.

curlyj

Programmer
Apr 8, 2001
10
GB
Hi,
Im trying to increment a negative number towards 0 onwards - and store the result in my array.
my negative number has 6 places and can only range from
-2.000000 to +2.000000
I have a start value, the step to increment by, and a limit - which resets back to start value

so I might have a start value of -1.351451 - with an increment of 0.000020 until a value of 1.300000 is reached = in which case the next value to be stored will be reset to the start value of -1.351451

im no good at explaining - so here is my code!!


start = -2.000000
limit = 2.000000
addon = 0.001000

result = start

For x = 0 To Counter
Bots$(x, 7) = val(result)
result = result + addon
If result > limit Then result = start
Next


the trouble is as i get near to 0 the results are whacky!!
im getting more then 6 decimal places, letters and - i cant find out what’s wrong!

any help would be pucka!

Many thanx!


QBasic is fun! Go see what I've done!
 
Can you just use a simple For...Next loop?

For a = start to finish step addon
...
..
Next a


You also appear to be setting a string array variable value to a numeric value!

Bots$(x, 7) = val(result)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Is this for school?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 

1. why are you using a string array for storing doubles to begin with? why not an array of type double?

2. why are you using val(result)? result isn't a string. if you are going to assign a string to a double, you better be using Format (if anything) and not Val.
 
i didnt think i could explaint it too well - here is my 2nd shot

Private Sub Command1_Click()
Dim start As Double, limit As Double, addon As Double
start = -2
limit = 1.1
addon = 0.1
Do Until start >= limit
start = start + addon
Debug.Print start
Loop
End Sub

which gives the following results
-1.9
-1.8
-1.7
-1.6
-1.5
-1.4
-1.3
-1.2
-1.1
-0.999999999999999
-0.899999999999999
-0.799999999999999
-0.699999999999999
-0.599999999999999
-0.499999999999999
-0.399999999999999
-0.299999999999999
-0.199999999999999
-9.99999999999994E-02
6.38378239159465E-16
0.100000000000001
0.200000000000001
0.300000000000001
0.400000000000001
0.500000000000001
0.600000000000001
0.700000000000001
0.800000000000001
0.900000000000001
1
1.1

it should hit -1, then -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2 etc

but its not!! can u help??

QBasic is fun! Go see what I've done!
 
Don't now why, but you can get around by doing like this.

Private Sub Form_Load()
Dim start As Double, limit As Double, addon As Double
start = -2
limit = 1.1
addon = 0.1
Do Until start >= limit
start = Round(start + addon, 5) ' the rounding places does not really matter on this case.
Debug.Print start
Loop
End Sub

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top