Author |
Topic: Rounding numbers (Read 2272 times) |
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #8 on: Apr 9th, 2015, 7:22pm » |
|
Oh! This is so much better than what I was doing. Below is what I was doing.
And, I was not separating the "PRINT" from the "FNusing" with the #5 . . . did not know that I could do this.
I do appreciate that you have provided a well-documented help file for learning file-writing in BBC BASIC, but I do find it one of the most challenging parts of my new endeavor.
not pretty, below. I have already modified this, but this is what I WAS doing.
Code: f& = OPENOUT("busadafiles\fabcard.dat") : IF f&=0 ERROR 53, "Cannot create file"
SWAP @hfile%(f&), @hfile%(5)
OSCLI "OUTPUT 5" : PRINT b$ : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT c$ : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Inner Diameter is "; BendsID$ : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Outer Diameter is "; OD : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Bends Radius is ";Degree; " degrees" : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Left tangent ";LT" ";"Right tangent is ";RT : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Left Tangent "; STR$(LT); " and the Right Tangent is "; RT : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Arc is "; arcnumber$ : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "CUT TUBE THIS LENGTH ------>"; STR$(centerlineLength) " " : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Circumference is "; CIRCUMFERENCE$ : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "Compass Bottom Arc At "; STR$(R - (OD / 2)); " inches" : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "COMPASS CENTER LINE ARC At "; STR$(R) " inches" : OSCLI "OUTPUT 0"
OSCLI "OUTPUT 5" : PRINT "COMPASS TOP ARC At "; STR$(R + (OD / 2)) " inches" : OSCLI "OUTPUT 0"
CLOSE #5
The only problem that I am having now is that I can't seem to get the PRINT FNusing function to allow me to write its value on the same line with a descriptive string.
For example, I want a file line to read . . .
Your circumference should be 34.56 in.
But every modification of this line (except a colon) just gives me a syntax error, and the colon does not help me keep a single line. I was hoping that a semicolon would work, but I get a syntax error. Code: PRINT #5, "Your circumference should be "
PRINT #5, FNusing("##.###", circumference)
|
| « Last Edit: Apr 9th, 2015, 8:13pm by CharlesB » |
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Rounding numbers
« Reply #9 on: Apr 9th, 2015, 9:21pm » |
|
on Apr 9th, 2015, 7:22pm, CharlesB wrote:| Below is what I was doing. |
|
That looks like code created by (or adapted from) the QB2BBC translator. As it says in the QB2BBC help: "the translated code can sometimes be difficult to read, and isn't necessarily much like what an experienced BBC BASIC programmer would write". If you're writing code yourself, it's not generally desirable to copy the kind of ugly code the translator produces!
Quote:| I can't seem to get the PRINT FNusing function to allow me to write its value on the same line with a descriptive string. |
|
String concatenation (using the + operator) is a fundamental feature of virtually all BASIC dialects:
Code: PRINT #file%, "Your circumference should be " + \
\ FNusing("##.###", circumference) Richard.
|
|
Logged
|
|
|
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #10 on: Apr 10th, 2015, 03:18am » |
|
Thank you Richard, you are right and I do remember reading that exact line in the qb translator. Yet, I am using the translator as a sort of tutorial . . . as I mentioned, I'm finding that file-handling is a bit difficult for me.
I am also slowly learning from, and using your provided example programs for files.
Thank you for your kind assistance.
|
|
Logged
|
|
|
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #11 on: Apr 10th, 2015, 9:32pm » |
|
Richard, not to take advantage of your time and kindness, but the following situation is another reason why I wanted rounded numbers and could not figure how to use the PRINT FNusing function as a solution.
I could think of no other way to produce a string with the proper "Price" value.
The code below is obviously wrong, but it is just one example of my frustrations.
Code:c$ = " "+ str$(Quantity%)+" "+ chr$(126)+ " ft "+ SelectedTubing$ + fnusing($##.##,Price) + "/ft. 20 ft lengths"
I was able to use the help of Eddy and D to produce a rounded value for "Price," convert it to a String variable, and thus be able to move on. At least the string would make sense.
Code:c$ = " "+ STR$(Quantity%)+" "+ CHR$(126)+ " ft "+ SelectedTubing$ + "$" +FixedPrice2$+ "/ft. 20 ft lengths"
The only problem was that their functions could produce a number as 40.9, where I would want a 40.90. This was simple to fix with some string manipulations, but not easy for me. This will really humble you!
In such a situation did I have a better alternative with PRINT FNusing?
|
| « Last Edit: Apr 10th, 2015, 9:34pm by CharlesB » |
Logged
|
|
|
|
Richey
New Member
member is offline


Gender: 
Posts: 35
|
 |
Re: Rounding numbers
« Reply #12 on: Apr 10th, 2015, 10:24pm » |
|
on Apr 9th, 2015, 04:55am, CharlesB wrote:I'm sure that this is an elementary question, and surely, I have searched the help files many times for this simple answer, but I can't find it.
I simply want to round off a number to two decimal places. I am aware of the PRINT FNUSING function and use it, but I am looking for something like USING$( ) that will actually convert (round off) a number.
So, may someone assist me to say, convert 34.8456123 to 34.85
Thank you, and please point me to the help files where I can learn to do this. Charles |
|
As someone who is still pretty much a novice, I tried to see if there was another way of doing this by practising what I have learnt about string slicing using MID$.
Here is my inelegant and clumsy effort. Apologies in advance for any errors / dodgy approaches:
Code:
INPUT "Floating Point number to be truncated ";test
INPUT "Rounded to how many decimal places ";places
count=0
convert$=STR$(test)
LenOfString=LEN(convert$)
FOR I=1 TO LenOfString
a$=MID$(convert$,I,1)
count=count+1
IF a$="." THEN EXIT FOR
PRINT a$;
NEXT I
PROC_decimal
END
DEF PROC_decimal
PRINT MID$(convert$,count,places+1)
ENDPROC
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Rounding numbers
« Reply #13 on: Apr 10th, 2015, 10:29pm » |
|
on Apr 10th, 2015, 9:32pm, CharlesB wrote:| I wanted rounded numbers and could not figure how to use the PRINT FNusing function as a solution. |
|
You seem to have got in your mind the false notion that FNusing() must be used in conjunction with PRINT. I wonder if this stems from your background with QBASIC (and other similar BASICs) in which it is indeed true that, stupidly, PRINT USING is the only usage.
But in BBC BASIC (with the FNUSING library), and also in Liberty BASIC, USING (or FNusing) is a regular function which can be used anywhere in your program; it is not at all tied to PRINT. Indeed a good way of thinking about it is that FNusing() has the same basic functionality as STR$(): they both take a numeric value and convert it into a decimal string.
The difference between STR$() and FNusing() is simply in how the formatting of the decimal number is controlled. With STR$() it is controlled using the special @% variable, whereas with FNusing() it is controlled by passing a special formatting string as the first parameter. But fundamentally the two functions do the same thing:
Code: INSTALL @lib$+"FNUSING"
a = 34.8456123
REM STR$ method (formatting controlled by @%):
@% = &102020A
a$ = STR$(a)
PRINT a$
REM FNusing method (with a formatting string):
a$ = FNusing("##.##", a)
PRINT a$
Quote:| The code below is obviously wrong, but it is just one example of my frustrations. |
|
You don't actually say what your "frustration" is. In your code you use both STR$() and FNusing(), which is absolutely fine, but it is worth bearing in mind that they are effectively doing the same thing, just with different ways of controlling the formatting.
Putting your code in the context of an entire program it seems to work perfectly well, so again I don't see where the "frustration" arises:
Code: INSTALL @lib$+"FNUSING"
Quantity% = 3
SelectedTubing$ = "3/4 inch "
Price = 23.3456789
c$ = " " + STR$(Quantity%) + " " + CHR$(126) + " ft " + \
\ SelectedTubing$ + FNusing("$##.##", Price) + "/ft. 20 ft lengths"
PRINT c$ I certainly don't think the functions offered by Edja and DDRM have any place here at all. Their contributions were well-meaning but have sent you in the wrong direction.
Richard.
|
|
Logged
|
|
|
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #14 on: Apr 10th, 2015, 11:55pm » |
|
Thank you again Richard. This is very helpful and I've learned a lot. My "frustrations" were just in my own inability to look at your help files (well written indeed), but to make proper sense of them so that I could correctly use them without introducing errors . . . again and again."
At first, I did not even understand that the FNusing could "operate" outside the realm of "PRINT". I mean, this is where I am.
Now I have something that I understand to work with, so again, thank you!
|
| « Last Edit: Apr 10th, 2015, 11:59pm by CharlesB » |
Logged
|
|
|
|
Edja
Developer
member is offline


Posts: 60
|
 |
Re: Rounding numbers
« Reply #15 on: Apr 11th, 2015, 12:36pm » |
|
Quote:| I certainly don't think the functions offered by Edja and DDRM have any place here at all. Their contributions were well-meaning but have sent you in the wrong direction. |
| True. Your further questions "unfolded" an objective that was clearly different. Eddy
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Rounding numbers
« Reply #16 on: Apr 11th, 2015, 1:08pm » |
|
on Apr 11th, 2015, 12:36pm, Edja wrote:| Your further questions "unfolded" an objective that was clearly different. |
|
The OP was no doubt trying to be helpful by distilling his problem down to what he thought were the basics. I encourage that, but there is a danger - as happened here - that the true objective will not be conveyed accurately.
Part of the problem is that the underlying issues are mathematical rather than specifically to do with programming. It's fundamental that 'rounding to two decimal places' has no well-defined meaning when applied to a number represented in binary floating-point. Indeed the concept of 'decimal places' only really applies to decimal numbers!
That is precisely why the built-in rounding functionality in BBC BASIC (in STR$ or in FNusing, which uses STR$ to do the work) happens only when converting a binary number to a decimal number.
Richard.
|
|
Logged
|
|
|
|
|