Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : is it possible to display long numbers ?
Author | Message | ||||
f1fco Senior Member Joined: 18/03/2012 Location: FrancePosts: 154 |
hello, I need to use long numbers (integers) like 123456789 I receive a string (m$) as "098765432" when I convert in number (Val(m$)) result was 9.87654e+07 is it possible to display and use the "real" number 98765432 ? thank you for help Pierre, from Nimes, south of France 73s de F1FCO |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5923 |
What version of MMBasic are you running. If it's a version that does integers: DIM n AS INTEGER
m$ = "098765432" PRINT VAL(m$) n = VAL(m$) PRINT n PRINT STR$(n,10) PRINT STR$(n,12) > RUN
98765432 98765432 98765432 98765432 > If you don't declare the number type, it will default to floating point. Jim VK7JH MMedit MMBasic Help |
||||
f1fco Senior Member Joined: 18/03/2012 Location: FrancePosts: 154 |
hello Jim, I use v4.5 on a Maximite but I try also v5 on a Micromite and it is ok with the type integer n! no issue with a 4.5 on a Maximite ? Pierre. 73s de F1FCO |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
Numbers on the Maximite are stored as floating point with a 24-bit mantissa. This means the maximum accuracy is normally 7 decimal digits and only 6 for numbers where the mantissa is greater than 8388607 |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5923 |
Using V4.5 on a Maximite, you can get a bit more using FORMAT$. S$="1234567890"
t = val(S$) print format$(t,"%10.0f") Note that this does NOT get around the limitations of 32bit floating point maths. The result from the above code will not display the same as the initial string. Depending on what you are doing, you may have to break the numbers up into usable sizes. A few years ago, I wrote a program to do integer maths with strings. ' integer maths by TassyJim 07 Feb 2012
a$ = "13" FOR i = 0 TO 15 ' demo of the four operations add( a$ , STR$( i ),res$ ) PRINT a$;" + ";i;" = ";res$ min( a$ , STR$( i ),res$ ) PRINT a$;" - ";i;" = ";res$ multy( a$ , STR$( i ),res$ ) PRINT a$;" * ";i;" = ";res$ div( a$ , STR$( i ),res$ ) PRINT a$;" / ";i;" = ";res$ NEXT i a$="1" FOR i = 1 TO 55 ' printing up to factorial 55. b$=STR$(i) multy(a$, b$, res$) PRINT b$;"! = ";res$;" (";LEN(res$);" )" a$=res$ NEXT i END SUB div( a$ , b$, c$ ) ' given a$ and b$, returns a/b in c$ LOCAL f, i, d, t, try$, nr$, a1$, b1$, a2$, z$ a1$=a$ : b1$=b$ : c$="" ' make copies of a$ and b$ to preserve originals IF VAL( b$ ) = 0 THEN ' divide by zero c$ = "error" ELSE FOR i = LEN( a1$ ) - LEN( b1$ ) TO 0 STEP -1 f = 0 nr$ = "0" z$=STRING$(i,"0") FOR d = 0 TO 9 md( b1$ + z$ , d, try$ ) big( a1$ , try$, t ) IF t THEN f = d nr$ = try$ ENDIF NEXT d c$ = c$ + STR$( f ) IF f > 0 THEN min( a1$ , nr$, a2$ ) a1$ = a2$ ENDIF NEXT i ENDIF trim (c$) END SUB SUB multy( a$ , b$, c$ ) ' given a$ and b$, returns the sum in c$ LOCAL i, h$, a1$, b1$, t$, d$, z$ a1$=a$ : b1$=b$ : c$="" ' make copies of a$ and b$ to preserve originals IF LEN( b1$ ) > LEN( a1$ ) THEN ' swap number for greater speed h$ = a1$ a1$ = b1$ b1$ = h$ ENDIF FOR i = LEN( a1$ ) TO 1 STEP -1 z$=STRING$(LEN( a1$ )-i,"0") md( b1$ , VAL( MID$( a1$ , i , 1 ) ) , t$) add( c$ , t$+ z$ ,d$) c$=d$ NEXT i trim (c$) END SUB SUB md( a$ , nr, c$ ) ' multiply a$ by a single digit, result in c$ LOCAL hold, carry, i carry = 0 : c$="" FOR i = LEN( a$ ) TO 1 STEP -1 hold = VAL( MID$( a$ , i , 1 ) ) * nr + carry carry = INT( hold / 10 ) c$ = STR$( hold MOD 10 ) + c$ NEXT i IF carry > 0 THEN c$ = STR$( carry ) + c$ END SUB SUB samelen (a$, b$) ' pads the shorter variable with leading zeros dif=ABS(LEN( a$ )-LEN( b$ )) IF dif>0 THEN z$=STRING$(dif,"0") IF LEN( a$ ) < LEN( b$ ) THEN a$ = z$+a$ ELSE b$ = z$+b$ ENDIF ENDIF END SUB SUB add( a$ , b$, c$ ) ' given a$ and b$, returns the sum in c$ LOCAL carry, hold, i,a1$, b1$ c$="" : a1$=a$ : b1$=b$ samelen a1$ , b1$ FOR i = LEN( a1$ ) TO 1 STEP -1 hold = VAL( MID$( a1$ , i , 1 ) ) + VAL( MID$( b1$ , i , 1 ) ) + carry carry = INT( hold / 10 ) x$=STR$( hold MOD 10 ) c$ = RIGHT$( x$,1) + c$ NEXT i IF carry > 0 THEN c$ = STR$( carry ) + c$ END SUB SUB min( a$ , b$, c$ ) ' given a$ and b$, returns the difference in c$ LOCAL a1$, b1$, h$, i, hold, borrow c$="" : s$="" :a1$=a$ : b1$=b$ ' initialise variables and preserve a$ and b$ samelen a1$ , b1$ IF a1$<b1$ THEN ' if result is going to be negative, swap a1 & b1 h$ = a1$ a1$ = b1$ b1$ = h$ s$="-" ENDIF FOR i = LEN( a1$ ) TO 1 STEP -1 hold = VAL( MID$( a1$ , i , 1 ) ) - VAL( MID$( b1$ , i , 1 ) ) + 10 - borrow borrow = 1- INT( hold / 10 ) c$ = STR$( hold MOD 10 ) + c$ NEXT i trim c$ c$ = s$+c$ ' add the sign END SUB SUB big( a$, b$, biggest ) ' biggest = 1 if a$ >= b$ LOCAL a1$, b1$ 'preserve a$ and b$ a1$ = a$ : b1$ = b$ biggest = 0 samelen (a1$ , b1$) IF a1$>=b1$ THEN biggest = 1 END SUB SUB trim (a$) ' given a$, leading zeros are stripped. a$ is altered. LOCAL s$ s$="" IF LEFT$(a$,1)="-" THEN ' if the number is negative, preserve the sign s$="-" a$=MID$(a$,2) ENDIF DO WHILE LEFT$(a$,1)="0" ' strip leading characters while they are "0" a$=MID$(a$,2) LOOP IF a$="" THEN a$="0" a$=s$+a$ ' replace the sign END SUB Jim VK7JH MMedit MMBasic Help |
||||
f1fco Senior Member Joined: 18/03/2012 Location: FrancePosts: 154 |
thank you Jim, I have tried FORMAT... but the result is not exactly the same as the string no problem, my project run perfectly on a Micromite with integers Pierre. 73s de F1FCO |
||||
Print this page |