|
Post by tsh73 on Feb 4, 2020 6:18:00 GMT
Because it is right way to do?
You can jump hoops especially if required by teacher, but there is more useful ways ( : own function)
NEXT is part of FOR..MEXT loop. So it is a loop you are instructed to use. Or you mean some other loop? Please explain what loops are allowed to use.
|
|
|
Post by B+ on Feb 4, 2020 15:27:36 GMT
Heck with loops really go primitive and use GOTO's Ha, ha, ha!
tsh73 have you been ghosted? where is students post? All that remains is title and it's new author.
To that guy or gal? I say it is a piece a cake to unpack a function back to main module code. Just take out first and last lines and give starting values to the parameters.
|
|
|
Post by tsh73 on Feb 4, 2020 17:12:48 GMT
Sorry this is outside of my active vocabulary. (yes I googled. I'd rather not to know).
I could invent some reasons as to where and why it's gone, but it will be just guessing. So I'd rather don't.
BTW I came up with no-loops solution. Just keep applying this line for next supposed place of comma, 7 lines good up to 9e21
if len(m$)>3 then m$=left$(m$, len(m$)-3)+","+mid$(m$,len(m$)-3+1)
|
|
|
Post by B+ on Feb 4, 2020 18:03:02 GMT
Sorry this is outside of my active vocabulary. (yes I googled. I'd rather not to know). I could invent some reasons as to where and why it's gone, but it will be just guessing. So I'd rather don't. BTW I came up with no-loops solution. Just keep applying this line for next supposed place of comma, 7 lines good up to 9e21 if len(m$)>3 then m$=left$(m$, len(m$)-3)+","+mid$(m$,len(m$)-3+1) Uhhh, what do you mean by "just keep applying" if you don't mean loop? And what if the number is a floater?
|
|
|
Post by tsh73 on Feb 4, 2020 18:38:36 GMT
|
|
|
Post by B+ on Feb 4, 2020 19:22:22 GMT
Cool! maybe one more?
for p = 1 to 21 m$ = str$(10^p) if len(m$)>3 then m$=left$(m$, len(m$)-3)+","+mid$(m$,len(m$)-3+1) if len(m$)>7 then m$=left$(m$, len(m$)-7)+","+mid$(m$,len(m$)-7+1) 'i really expected 6 here if len(m$)>11 then m$=left$(m$, len(m$)-11)+","+mid$(m$,len(m$)-11+1) if len(m$)>15 then m$=left$(m$, len(m$)-15)+","+mid$(m$,len(m$)-15+1) if len(m$)>19 then m$=left$(m$, len(m$)-19)+","+mid$(m$,len(m$)-19+1) if len(m$)>23 then m$=left$(m$, len(m$)-23)+","+mid$(m$,len(m$)-23+1) if len(m$)>27 then m$=left$(m$, len(m$)-27)+","+mid$(m$,len(m$)-27+1) 'if len(m$)>31 then m$=left$(m$, len(m$)-31)+","+mid$(m$,len(m$)-31+1) 'if len(m$)>35 then m$=left$(m$, len(m$)-35)+","+mid$(m$,len(m$)-35+1) 'if len(m$)>39 then m$=left$(m$, len(m$)-39)+","+mid$(m$,len(m$)-39+1) print m$ next
|
|
|
Post by B+ on Feb 4, 2020 20:05:31 GMT
Hi tsh73,
At first when you said no loop, I was imagining something like this:
for p = 1 to 23 m$ = str$(-10^p) copy$ = m$;".33" 'if len(m$)>3 then m$=left$(m$, len(m$)-3)+","+mid$(m$,len(m$)-3+1) 'if len(m$)>7 then m$=left$(m$, len(m$)-7)+","+mid$(m$,len(m$)-7+1) 'i really expected 6 here 'if len(m$)>11 then m$=left$(m$, len(m$)-11)+","+mid$(m$,len(m$)-11+1) 'if len(m$)>15 then m$=left$(m$, len(m$)-15)+","+mid$(m$,len(m$)-15+1) 'if len(m$)>19 then m$=left$(m$, len(m$)-19)+","+mid$(m$,len(m$)-19+1) 'if len(m$)>23 then m$=left$(m$, len(m$)-23)+","+mid$(m$,len(m$)-23+1) 'if len(m$)>27 then m$=left$(m$, len(m$)-27)+","+mid$(m$,len(m$)-27+1) 'if len(m$)>31 then m$=left$(m$, len(m$)-31)+","+mid$(m$,len(m$)-31+1) 'if len(m$)>35 then m$=left$(m$, len(m$)-35)+","+mid$(m$,len(m$)-35+1) 'if len(m$)>39 then m$=left$(m$, len(m$)-39)+","+mid$(m$,len(m$)-39+1) 'print m$, recur$(copy$) print recur$(copy$) next
function recur$(x$) v = val(x$) if abs(v) < 1000 then recur$ = x$ : exit function else c = instr(x$, ",") if c then tail$ = mid$(x$, c) : head$ = mid$(x$, 1, c - 1) if len(head$) > 3 then head$ = left$(head$, len(head$) - 3);",";mid$(head$, len(head$) - 2) recur$ = recur$(head$ + tail$) else d = instr(x$, ".") if d then tail$ = mid$(x$, d) : head$ = mid$(x$, 1, d - 1) if len(head$) > 3 then head$ = left$(head$, len(head$) - 3);",";mid$(head$, len(head$) - 2) recur$ = recur$(head$ + tail$) else x$ = left$(x$, len(x$) - 3);",";mid$(x$, len(x$) - 2) recur$ = recur$(x$) end if end if end if end function
|
|
|
Post by tsh73 on Feb 5, 2020 7:44:46 GMT
Recursive functions are tricky (at least for me) so I don't quite found where it dies but putting in
m$ = str$(-10^p*0.123456789) made it hang over about 8-9 digits
It might be related to facts that * double has only about 16 digits * STR$ on floats returns about 7 digits out of 16
so anyway you cannot store 10000000000000000000000.33 in numeric variable in JB - so nowhere to get this number in the first place
x=10000000000000000000000.33 print x print x- 10000000000000000000000.33 print x- 10000000000000000000000 print x- 10000000000000000123456
See?
1.0e22 0.0 0.0 0.0
|
|
|
Post by B+ on Feb 5, 2020 14:55:06 GMT
Try this: m$ = str$(int(-10^p*0.123456789))
|
|
|
Post by tsh73 on Feb 5, 2020 20:28:13 GMT
Aha So then it gets string "-1.23456789e8.33" condition if abs(v) < 1000 then fails, so no return. But dividing into head and tail ends up with "-1" ".23456789e8.33" And condition if len(head$) > 3 fails, so recur$ = recur$(head$ + tail$) is called on exact same sting as first time. Hence loop.
so changing
if len(head$) > 3 then head$ = left$(head$, len(head$) - 3);",";mid$(head$, len(head$) - 2) recur$ = recur$(head$ + tail$)
to
if len(head$) > 3 then head$ = left$(head$, len(head$) - 3);",";mid$(head$, len(head$) - 2) recur$ = recur$(head$ + tail$) else recur$ = x$ : exit function end if
removes that loop
As a side note, p>20 with
m$ = str$(int(-10^p*0.123456789)) shows weird artifacts:
p=21 -123,456,788,999,999,995,904.33 p=22 1,234,567,890,000,000,057,344.33 p=23 -12,345,678,899,999,999,524,864.33
|
|
|
Post by B+ on Feb 5, 2020 21:00:24 GMT
Hi tsh73, Not sure where "-1.23456789e8.33" is coming from but I will take your word on improvement... thanks, my code updated (not seeing a difference but if running an extra call to sub then good to correct even if it costs more lines of code). "show weird artifacts" yes, I noticed but I will take that over the program hanging and then running out of memory. I think we have better code for adding commas to number? but fun to see if can do without a loop.
|
|