Liberty BASIC Community Forum
« Help for if statement »

Welcome Guest. Please Login or Register.
May 28th, 2017, 10:54pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Help for if statement  (Read 324 times)
Ernie
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Help for if statement
« Thread started on: Dec 21st, 2015, 4:40pm »

Hi to all,

Hi to all

I have the following problem :

I have a data list in a csv file with one column.
The number format is double with 7 decimals.
My goal is to copy the data into another file and round every tenth number ie 1st,10th,20th,30th,etc...
The problem is that the first original number is well rounded but it is copy again in its original form as number 2.
I think that the problem come from the "print #Col1,n" which is not at its correct place, but I could not fix that
after many attempts.


Code :

open "Spline_integer1.txt" for output as #Col1

open "C:\Users\OriginalData.csv" for input as #csv



i=1

while not(eof(#csv))

line input #csv, row$

n$ = Word$(row$, 1,";") : n = Val(n$)

'--------------------------------------------------------------

for y= 1 to 1000 step 10
if i=y then print #Col1,round(n,0)
next y

print #Col1,n

i=i+1

wend

close #Col1
close #csv

end

function round(x,places)
'positive "places" rounds to whole number places,
'negative "places" rounds to fractional places
round = sgn(x)*int((abs(x)*10^(-1*places))+0.5)*(10^places)
end function 'round

function sgn(x)
if x<0 then
sgn = -1
else
sgn = 1
end if
end function 'sgn

----------------------------------
Original file with 30 data
2.01616 a
2.25925 b
2.5611
2.91259
3.30459
3.72797
4.17361
4.63239
5.09518
5.55285
5.99628
6.41771
6.8148
7.1866


Final file with rounded data

2 a Rounded data. OK
2.01616 a Original data. Must not be copy again.
2.25925 b Original data
2.5611
2.91259

Final file must be :

2 a Rounded data. OK
2.25925 b
2.5611
2.91259
.......

User IP Logged

btgraham
New Member
Image


member is offline

Avatar




PM


Posts: 38
xx Re: Help for if statement
« Reply #1 on: Dec 21st, 2015, 6:40pm »

Ernie:

Try this:-

i=1
y=0
while not(eof(#csv))
line input #csv, row$
n$ = Word$(row$, 1,";") : n = Val(n$)

if i=1 or i=y then
print#Col1,round(n,0)
y=y+10
else
print#Col1,n
end if
i=i+1
wend

'etc........

Did you realize that you would be rounding the 1st,11th,21st with the y=1 to 1000 step 10?

User IP Logged

Barry Graham
Melbourne, Australia
Ernie
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Help for if statement
« Reply #2 on: Dec 21st, 2015, 6:54pm »

Hi Barry,

Thank you for your help. I'll try your code.

My goal is effectively to round the 1st, the 10th,the20th,etc..

Ernie
User IP Logged

tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1151
xx Re: Help for if statement
« Reply #3 on: Dec 21st, 2015, 7:23pm »

We're always glad to help people, BUT... it helps if you spell out more carefully what you want to do.

'Please put BASIC code and text files between [code/ [/code] markers. It's much easier for us to read. That's the '#' symbol on the forum editor.

'Don't know what you mean by 'double with 7 decimals'. Do you mean 7 characters including a decimal point?
'Col1 and Coll look same on screen- bad choice of name.
'Why the stuff with word$ if the csv file represents just one column?
'We don't see why you are looking for 1st, 10th, 20th. Logic says 0,10,20 or 1,11,21...

Did you use the debugger? An alternative is to add lines to print the output to screen as well as to file.

My take on what you want was..
Code:
open "Spline_integer1.txt" for output as #Col1
open "OriginalData.csv"    for input  as #csv

i =1

while not( eof( #csv))
    print i,
    line input #csv, row$
    n =Val( row$)
    if ( i mod 10) =0 then #Col1 int( n): print int( n) else #Col1 n: print n, using( "#.######", n)
    i =i +1
wend

close #Col1
close #csv

end
 

...which produces on screen
Code:
1             2.01616       2
2             2.25925       2.25925       2.259250
3             2.5611        2.5611        2.561100
4             2.91259       2.91259       2.912590
5             3.30459       3.30459       3.304590
6             3.72797       3.72797       3.727970
7             4.17361       4.17361       4.173610
8             4.63239       4.63239       4.632390
9             5.09518       5.09518       5.095180
10            5.55285       5
11            5.99628       5.99628       5.996280
12            6.41771       6.41771       6.417710
13            6.8148        6.8148        6.814800
14            7.1866        7.1866        7.186600
Done
 

and as a file...
Code:
2
2.25925
2.5611
2.91259
3.30459
3.72797
4.17361
4.63239
5.09518
5
5.99628
6.41771
6.8148
7.1866 
User IP Logged

tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1151
xx Re: Help for if statement
« Reply #4 on: Dec 21st, 2015, 7:43pm »

Incidentally, your rounding function is neat if not entirely necessary.
Here's a demo of it working on random numbers in range 0 to 99.99999..
Code:
for j =-3 to 2 step 1
    if j <0 then print j *-1; " decimal places." else print " as nearest integer."
    for i =1 to 10
        z =100 *rnd( 1)
        print z, round( z, j)
    next i
    print
next j

wait

function round( x, places)
    'positive "places" rounds to whole number places, negative "places" rounds to fractional places
    round = sgn( x) *int( ( abs( x) *10^( -1 *places)) +0.5) *( 10^places)
end function 'round

function sgn( x)
    if x <0 then sgn =-1 else sgn =1
end function 'sgn
 

giving output like
Code:
3 decimal places.
22.0541554    22.054
74.2061168    74.206
47.5411974    47.541
41.6661977    41.666
7.14868799    7.149
55.2423648    55.242
29.1226894    29.123
54.130663     54.131
13.9743198    13.974
89.858501     89.859

2 decimal places.
27.4735419    27.47
66.7659817    66.77
84.4365914    84.44
11.5613262    11.56
15.2872045    15.29
45.6731997    45.67
41.9398169    41.94
99.3340451    99.33
59.1868115    59.19
71.6499235    71.65

1 decimal places.
0.52667887    0.5
40.1541107    40.2
14.5264487    14.5
97.6565619    97.7
67.5756676    67.6
35.1457731    35.1
97.6729697    97.7
97.8659354    97.9
34.4678453    34.5
22.7455868    22.7

 as nearest integer.
3.87108652    4
0.40975565    0
13.6207751    14
61.0044107    61
73.3194461    73
82.133861     82
3.21341722    3
84.8550913    85
42.6268119    43
46.853819     47

 as nearest integer.
6.89238944    10
87.3879293    90
57.9990764    60
15.5542335    20
92.6508043    90
93.7622709    90
35.9502939    40
57.8757491    60
73.3885595    70
30.0192849    30

 as nearest integer.
30.0397397    0
4.01857771    0
59.3044718    100
86.6301415    100
64.4693159    100
37.3603521    0
15.4226469    0
81.4284238    100
90.8122483    100
14.8032997    0
 

The 'using( "##.####', x)' makes it easier to align the results, and fill in the trailing zeroes where needed...
User IP Logged

Ernie
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Help for if statement
« Reply #5 on: Dec 21st, 2015, 9:29pm »

Hi Barry,

I have tested your code and it gave me the results I hoped.
It is vey fast. I have 37940 data in my file.

Thanks again.

Ernie
User IP Logged

Ernie
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Help for if statement
« Reply #6 on: Dec 21st, 2015, 9:31pm »

Hi tenochtitlanuk,

Thank you for your code and your advices.

I apologize for my approximative vocabulary as english is not my mother language.
I understand that putting code between code markers will make it easier to read but I was not sure how to make it. Will look at this point.

About double number with 7 decimals, I mean numbers like 2.0161600,8.4011200 with 7 decimals after the dot. Is that right?

About Col1 and Coll looking same on screen, I don't really understand what you mean but I'll consider your advice, as well for the numerotation 0,10,20 or 1,11,21...

About the debugger, I always use it to understand what's going on and from where errors come. For this program, I do also have printed results to the screen for immediate reading.

About your code and the resulting file : What I meant was that I need to round the numbers as 0th for the first number if we count from 0, and the second number would be the 10th. Can you tell me how to change the code to do that?

Code:
2
2.25925
2.5611
2.91259
3.30459
3.72797
4.17361
4.63239
5.09518
5
5.99628-number to round
6.41771
6.8148
7.1866
 


Thank you.
User IP Logged

tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1151
xx Re: Help for if statement
« Reply #7 on: Dec 22nd, 2015, 04:41am »

Don't worry about your ( excellent) English... I enjoy attempting other people's language but am ashamed how bad the anglophone world is generally at realising all is not English.
And Merry Christmas, welcome to this forum.

The comment about a variable called 'Col1' was simply that in editing your program the screen font made it look like 'Coll' with the letter '1' and not number 'l', so I was using two different names!

You might find that the 'using( "##.###", x)' function is simpler. It specifies how many digits you want in front and after the decimal point. So for up to 3 numbers before the decimal point and seven after it would use "###.#######".
An example which puts together a series of format strings and shows them in use ...
Code:
for j =0 to 9
    format$ ="###." +dp$
    for k =1 to j
        format$ =format$ +"#"
    next k
    for i =1 to 10
        z =100 *rnd( 1)
        print z, using( format$, z)
    next i

    print
next j

wait
 


If you really want the first, then 10th,20th,etc use a line like..
Code:
if ( i mod 10) =0 or ( i =1) then #Col1 int( n): print int( n) else #Col1 n: print n, using( "#.######", n) 
« Last Edit: Dec 22nd, 2015, 04:43am by tenochtitlanuk » User IP Logged

Ernie
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Help for if statement
« Reply #8 on: Dec 22nd, 2015, 5:03pm »

Hi tenochtitlanuk,

Thank you for your help.
Wish you a Merry Christmas.

Ernie
User IP Logged

kokenge
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 24
xx Re: Help for if statement
« Reply #9 on: Dec 23rd, 2015, 3:15pm »

What's wrong with print using?

#######.### for 3 place round
#######.## for 2 place round

print using("########.###",7.14868799) gives 7.149

For whole numbers, strip the decimal and move it to the front and multiply the result of val(using("#",n)) * 100

Maybe I don't understand the using statementhuh

I guess I'm asking more than giving a solution...
Thanks

User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2262
xx Re: Help for if statement
« Reply #10 on: Dec 23rd, 2015, 3:59pm »

Using is primarily for formatting numbers for output, not clamping values for calculation.

If you actually do a PRINT on just using("#", n), you'll see that it likely prints out a percent sign first.


If you want to clamp a number to a whole number, use INT().

Code:
n = 12.458648
print int(n) * 100 



If you want to do the multiplication first, then, well, do it first.

Code:
n = 12.458648 * 100
print int(n) 



(As an example, if you do still want to use USING() for some reason: )

Code:
n = 12.658648
a$ = using("#", n)
if left$(a$, 1) = "%" then a$ = right$(a$, len(a$) - 1)
print val(a$) * 100 
« Last Edit: Dec 23rd, 2015, 4:02pm by Chris Iverson » User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
kokenge
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 24
xx Re: Help for if statement
« Reply #11 on: Dec 23rd, 2015, 4:46pm »

Here is a quick program I did to test it out:
I can only get a % on negative numbers..
If it contains negatives it's a quick fix to this:

Code:
x$ = "#########################"
for p = 0 to 5
print p;" decimal places ---------"
 p$ = "####." + left$(x$,p)
 for i = 1 to 10
	a = rnd(0) * 1000
print a;chr$(9);using(p$,a);chr$(9);int(a)
next i
next p

print " ---- whole numbers ---"
for i = 1 to 10
 a = rnd(0) * 1000

a$ = str$(a)
a$ = ".";word$(a$,1,".") + word$(a$,2,".")
a1 = val(a$)
print a;chr$(9);val(using("#",a1)) * 100;chr$(9);int(a)
next i 


Thanks for the help.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls