Dear support
I don't find in the helpfile from EBASIC the functions :
/* Round a numeric value to a specified number of decimal places */
x = round(numeric expression, range)
/* Convert a numeric expression to an integer value. */
x = int(numeric_expression)
/* Return the fractional part of a floatingpoint number. */
h = fraq(float_expression)
I have this function need for this formula :
aan_tijd = roiund(lengte / setuptijd)
Can somebody create this function of where can I find it?
Kind regards
Stephane
Quote from: Techno on September 23, 2008, 08:44:25 AM
/* Convert a numeric expression to an integer value. */
x = int(numeric_expression)
From EBasic help file:
QuoteSyntax
integer = INT(num)
Description
Converts a floating point value to an integer.
Parameters
num - A DOUBLE or FLOAT
Return value
If num is a DOUBLE then the return is an INT64 otherwise an INT.
Remarks
INT truncates the decimal portion. This behavior is different than assigning a floating point value to an integer variable where the rounding mode will return the largest integer.
Example usage
PRINT INT(1.56)
Larry
Larry already explained the INT() functions. Here's the rest. In this example ROUND() function rounds to number of decimal places but there are also ROUND() functions that might round to a number of significant places.
OPENCONSOLE
SETPRECISION(9)
INT i
DOUBLE d1
d1=3.141592653
PRINT "Original number = ",d1: PRINT
PRINT "INT() function = ",INT(d1)
PRINT "FRC() function = ",FRC(d1): PRINT
FOR i=9 TO 0 STEP -1
PRINT "ROUND(",STR$(i),") = ",ROUND(d1,i)
NEXT i
PRINT: PRINT "Press any key to close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END
SUB FRC(DOUBLE d),DOUBLE
RETURN d-INT(d)
ENDSUB
SUB ROUND(DOUBLE d, INT n),DOUBLE
SETPRECISION(n)
RETURN VAL(STR$(d))
ENDSUB
Barney
i modified Barney's example slightly and get some interesting results (i think due to the way CPU's represent floating point numbers internally) .
Try a number with more than 12 decimal nrs, and see what you get...
OPENCONSOLE
SETPRECISION(15)
INT i
DOUBLE d1
input "Give me a decimal number :", d1
PRINT "Original number = ",d1: PRINT
PRINT "INT() function = ",INT(d1)
PRINT "FRC() function = ",FRC(d1): PRINT
FOR i=9 TO 0 STEP -1
PRINT "ROUND(",STR$(i),") = ",ROUND(d1,i)
NEXT i
PRINT: PRINT "Press any key to close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END
SUB FRC(DOUBLE d),DOUBLE
RETURN d-INT(d)
ENDSUB
SUB ROUND(DOUBLE d, INT n),DOUBLE
SETPRECISION(n)
RETURN VAL(STR$(d))
ENDSUB
Strange isn't it? the last nr is never valid...
Anyone can comment,
Cheers,
Frank
IEEE numbers are not precise after 12 or 13 decimal places.
SUB ROUND(DOUBLE d, INT n),DOUBLE
SETPRECISION(n)
RETURN VAL(STR$(d))
ENDSUB
Interesting solution. But wouldn't it be simpler just to normalize the number?
return int(d * 10.0^n)/10.0^n
I don't think he was referring to the display of the number, just truncating it to a number of decimal places. If you just wanted to display a certain number of decimal places then use the USING command.
Paul.
i hope this helps you, simple is sometimes the best
Your X, i have set to 2 decimal places'
the number can be any number you think of and as many decimal places which can be displayed
the number in the example has 16 decimal places rounded down to two decimal places, with , X
OPENCONSOLE
int x
x=2
number=12.7891074321453421
SETPRECISION x
print number
print "press ESCkey to exit"
label getesc
k$=inkey$
if k$<> chr$(27) then goto getesc
END
CLOSECONSOLE
number now equals 12.78
you set , X, as the number of decimal places you require.
From your post i assume you wanted X decimal places?
hope it helps you.
regards
Hugh
Hugh
You're solution only changes how the number is displayed. It does not round off the actual number.
The function from above
SUB ROUND(DOUBLE d, INT n),DOUBLE
SETPRECISION(n)
RETURN VAL(STR$(d))
ENDSUB
actually returns the rounded off number.
So what you are doing and what the function is doing are two different things.
Run this little bit of code to see the difference.
openconsole
def a as float
a=2.35647291
setprecision 2
print a
a=2*a
print a
print "If a was rounded the answer should have been 4.72 instead of 4.71"
a=2.35647291
setprecision 2
a=val(str$(a))
print a
a=a*2
print a
print "see the difference?"
do:until inkey$<>""
closeconsole
end
Larry
Hi Larry,
Just noticed your reply, my error, unless i am mistaken, SETPRECISION 2 rounds the number 2 decimal places, in this case it rounded it up to 12.79.
I typed in my previous post it was 12.78, error on my part.
the following code displays original number, and then displays it rounded UP.
do you mean that the number still retains its exact value before and after ,SETPRECISION?
i have used the original number in the code below
OPENCONSOLE
number=12.7891074321453421
SETPRECISION 16
print "Original number is ",number
PRINT
PRINT"and multiplied by itself = ",number*number
SETPRECISION 2
PRINT
print"Precision set to 2, number now rounded to " ,number
PRINT
print"Number now multiplied by itself = ",number*number
PRINT
print "my error, in my original reply, i should have typed number = 12.79, and not 12.78"
PRINT
PRINT "Hit Any Key To Exit"
do:until inkey$<>""
closeconsole
end
or am i wrong again?.
I do understand what you mean.
regards
Hugh
Hugh
You are missing what I meant, I believe.
My point was, and is, that setting precision does not change the number. It only changes how it is displayed.
In the example below I setpricision to 16 and print "number"
Then setprecision to 2 and "show" it rounded.
Then I setprecision back to 16 and print "number" again.
Number's value has not been changed by setting precision. Only the way it is displayed is changed.
OPENCONSOLE
number=12.7891074321453421
SETPRECISION 16
print "Original number is ",number," with precision set to 16"
SETPRECISION 2
PRINT
print"Precision set to 2, number now rounded to " ,number
PRINT
SETPRECISION 16
PRINT
print "Original number is still ",number," with precision set back to 16"
PRINT
PRINT "Hit Any Key To Exit"
do:until inkey$<>""
closeconsole
end
Larry
Thanks for that Larry,
I do understand exactly what you mean now.
regards
hugh
Looks like there's a bit of confusion created by my usage of SETPRECISION() function. I agree that it actually sets the display of numbers and it leaves the actual representation of numbers intact but it also affects the STR$() function. My ROUND() function uses this to get the number rounded to a certain number of decimal places with the rest thrown away (truncated). So, if for example one rounds 3.141592653 to five decimal places i.e. ROUND(3.141592653,5) the resulting number will be a DOUBLE and it will have a value of 3.141590000.
Easily seen with one small change in the ROUND() function.
SUB ROUND(DOUBLE d, INT n),DOUBLE
DOUBLE f
SETPRECISION(n)
f=VAL(STR$(d))
SETPRECISION(10)
RETURN f
ENDSUB
But I agree with Paul. His approach is better because it is purely mathematical. It does not rely on a specific function of a particular language.
Barney
You're right Barney
The STR$ function does exactly the same thing that the PRINT statement does; it changes how the number appears and not it's actual value.
As you've show, using the VAL function in conjunction with the STR$ function you can opbtain a number that has actually been rounded.
Larry
hello barney, i have placed two REMS beside your original post in two lines.
The REMS are actually questions.
PRINT "INT() function = ",INT(d1): ' d1 passed to -sub ROUND(DOUBLE d, INT n),DOUBLE and d returns value
PRINT "FRC() function = ",FRC(d1): PRINT: ' d1 passed to SUB FRC(DOUBLE d),DOUBLE and d returns value
am i correct in that assumption about 'd' returning the value?.
Regards
hugh
Nope. You are wrong in your assumption, Hugh.
"d" is a variable (of DOUBLE type) receiving the value from the calling program. It is a variable that is created on the stack and "lives" only while the function is executing. When the called function ends execution the result is placed onto the stack and it is up to the calling program to save the result somewhere, most likely it'll be another variable, which does not have anything to do with the "d" variable used in the called function.
So, in FRC(DOUBLE d),DOUBLE the value of the "d1" variable is passed to a "d" variable, but it is not the "d" variable that contains the result of the function. Once you returned from the function the "d" variable does not exist anymore and you must either save the result of the function to a variable in your calling program or use that result straight away, which is exactly what I did inside a PRINT statement.
Barney
Thanks Barney,
That explains it perfectly.
now i fully understand it.
Regards
Hugh