views:

81

answers:

4

I have this line of code which I want to concatenate -or at least solve the loop problem...

test = 1 - ("0." & thisnumber(0) & thisnumber(1) & thisnumber(2))

I want this to have a loop in it...

-Increasing thisnumber()

Until it gets to about 500,

Can some implement a loop into this?

Or suggest a way...

Thanks a lot..

James :)

EDIT:

So if I had values thisnumber(0) = 1, thisnumber(1) = 5, thisnumber(2) = 0, thisnumber(3) = 7... It would do 1 - 0.1507... (But I want a loop so it does all 500 without me typing them all out) -I'm wanting 1,000,000 so it would be a huge problem.

A: 

Use a DO ... WHILE loop

Raj More
A: 
Dim d As Integer
Dim val As String
val = "0."
For d = 0 To 500
    val = val & d
Next d
test = 1 - Convert.ToDouble(val)
Kyra
Ok, thats alright But, I'd have to right it out (till 500) I want it to do it all. -inside the 'test ='
James Rattray
@James Rattray: None of the .NET built-in data types will give you precision of 500 decimal places. May I ask why you need such a high precision?
0xA3
Well are you wanting test to equal += to the right of test? Right now, the way you had it, would keep replacing the value of test in which case the final number would be equal to 1-(0.500501502). At least that is what it seems like. what exactly do you want.
Kyra
I want the brackets to be looped, simple. -Too increase the array position... And I want to get Pi to 500...
James Rattray
So if I had values thisnumber(0) = 1, thisnumber(1) = 5, thisnumber(2) = 0, thisnumber(3) = 7... It would do 1 - 0.1507.
James Rattray
That code doesn't solve the problem as stated in the question.
Dan Puzey
I just changed the code. But this is the best I can think of. And @Dan Puzey I misunderstood his question and was waiting on clarification before I changed my answer.
Kyra
Your code still doesn't solve the question as stated...
Dan Puzey
Thats good, but will overflow =/
James Rattray
+1  A: 

The string concatenation is super confusing here. I think you're actually trying to do arithmetic, yes? You want a loop in which you're incrementing both the integer you pass to thisnumber() and the power of ten you're dividing by. So you have 1 - thisnumber(0)/10 - thisnumber(1)/100 / thisnumber(2)/1000 and so on. You should be able to do that with a loop once you stop thinking about building a string.

Update: what type are you planning to use for test? Do you understand how many decimal places of precision it can hold if it's a number? If it's a string, what are you going to do with it once you have it?

Kate Gregory
I want the brackets to be looped, simple. -Too increase the array position... Can you help?
James Rattray
@James: stop posting the same comment to every response! If people aren't answering your question correctly then maybe it's not as simple as you assume...
Dan Puzey
(And @Kate, I'm not sure it's a string concatenation as such... integer-concatenation, ish?)
Dan Puzey
Kate Gregory
A: 

Something like this, maybe... (Excuse my VB syntax, it's been a while)

dim num as double = 0.0

for i as integer = 0 to 500
    num += thisnumber(i) / (10 ^ (i + 1))
next 

test = 1 - num

However... this will overflow way before you get to 500 digits, and I still have to wonder why your input is in this format in the first place...

EDIT: based on the OP's comments, here's a version minus overflow...

dim num as double = 0.0
dim factor as double = 1.0;

for i as integer = 0 to 500
    factor /= 10
    num += thisnumber(i) * factor
next 

test = 1 - num

This version won't overflow, but you'll run into decimal precision issues along the way. If, as the OP suggests, this is about finding Pi to high accuracy, there are probably better ways - but without knowing the actual problem, I'm not sure it's worth going into detail.

Dan Puzey
Yeah thats what I mean, I'm doing it my way to stop it overflowing... -Getting Pi to alot of Dp's.So thanks, but no thanks :P
James Rattray
But you can't stop the overflow by using your loop method: if you want it as a number to "lots of DPs," you're going to need something other than a standard data type. Also, if that's what you're trying to do, you should perhaps try *posting that as your question* so that people know what your real problem is.
Dan Puzey
Also: you're going to run into decimal precision issues anyway, so I suspect you're not going to find the solution you're after. I'll post an edit that avoids the overflow in a sec, but you'll be still screwed on accuracy...
Dan Puzey
Even the second version would not yield the desired result. I guess it makes no difference whether you iterate from 1 to 20 or from 1 to 500...
0xA3