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 : cut the last digits from a number
Author | Message | ||||
plasma Guru Joined: 08/04/2012 Location: GermanyPosts: 437 |
hi , how to cut the last digits from a number ? a= 1.550001 but i need a = 1.55 anyone ? thx |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
I would use: A$=STR$(a) 'Convert numerical variable into a string Number=VAL(MID$(A$,1,4)) 'Extract x.xx from the string and save it in 'Number' Probably easier ways - others will chime in... EDIT: This is not working, so I am thinking of something else. I expected it would - I will try this on MM - I am using the uM chip, just cos it is already hooked to the PC at the moment... EDIT: I expect that FORMAT is the command you will need... (reading...) Smoke makes things work. When the smoke gets out, it stops! |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
a= 1.550001 but i need a = 1.55 a = Int(a * 100) a = a/100 Jman |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
I get a result of 1.549998 from the uM chip using jman's code. I will try this on the full size MM. I am guessing that plasma wants only the first two significant figures, so wants 1.55, but not the rest of the number. EDIT: OK, full-size MM gives interesting results too. Using jman's code: A=1.550001 Print A A=INT(A*100) A=A/100 Print A Result is: 1.55 1.55 MM is automatically truncating 1.550001 right from the off. If I use A=1.2345678, and run the same code, the result is: 1.2345678 1.23 EDIT: the above works on the uM chip too, so long as I use A=1.2345678 As soon as I use A=1.550001, I get odd results from the uM chip, and auto-trimming of A in the full size MM. EDIT: A=1.55000678 is changed to 1.55 by the MM - any more then three zeros in the number, and everything to the right of that is ignored. A=1.23000456 results in A being equal to 1.23 ...interesting... Smoke makes things work. When the smoke gets out, it stops! |
||||
plasma Guru Joined: 08/04/2012 Location: GermanyPosts: 437 |
micromite source where is no format function in micromite . for r = 1 to 10 a= a+ 0.11 print a next shows the "same" for me , its floats inacuracy or ? |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6101 |
All numbers are stored as floating point so very few will dicplay as expected. What you need is a Format function for uMites's Here is one I prepared earlier: function uFormat$(x,p) ' given a number (x) and the required number of decimal places (p) ' returns formatted string. Negative and zero p is permitted local x1, f$ f$=str$(cint(x*10^p)) if p>0 then uFormat$=left$(f$,len(f$)-p)+"."+right$(f$,p) elseif p = 0 then uFormat$=f$ else uFormat$=f$+string$(abs(p),"0") endif end function This solves the display issue but you still might get caught out if you compare two numbers which appear to be equal. Jim VK7JH MMedit  MMBasic Help |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3805 |
We've covered the inherent behaviour of fp (floating point) numbers before. You can live with it in a variety of ways but it will always be there if you're outside what can be represented exactly. (It's the same on every computer using fp but often they use 64 or 80 bits rather than 32 so you tend to notice it less with those other computers.) John |
||||
psergiu Regular Member Joined: 09/02/2013 Location: United StatesPosts: 83 |
Fo the same code & same numbers, i don't think uM and MM should give different results - are you sure you put the same number of zeroes in both uM and MM ? If yes, we have a new uM bug for Geoff :) |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
uM chip with same code gave 1.549998, whereas CMM unit with same code gave me 1.55 Smoke makes things work. When the smoke gets out, it stops! |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3196 |
Part of this discrepancy would because I used different code to turn a float number into a string in the Micromite, In the Maximite I used the sprintf() routines provided by Microchip - but they took up 12K of flash!! So for the Micromite I built my own. The floating point routines are the same and the resultant number is the same, it is just that the conversion of the value to a string is a little different when we are at the extremeness of the 32 bit float range. It is not that one is accurate and the other is not but more just different interpretations of the last few digits on an indistinct value. Geoff Geoff Graham - http://geoffg.net |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Rather odd If we use 1.550001 > Print 1.550001 * 100 155.0001 > Print INT(155.0001) 155 > Print 155 / 100 1.5499998 !!!! What > > Print 155 / 10 15.5 !!! This one is correct This would suggest a bug in the divide command :( Regards Jman |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6101 |
Not a bug, just the way floating point numbers can behave. You need to decide what resolution you want and use a function like the one I posted yesterday to display what you want to display. Jim VK7JH MMedit  MMBasic Help |
||||
OA47 Guru Joined: 11/04/2012 Location: AustraliaPosts: 926 |
Plasma, had the same frustration when trying to display info on tera term with the micromite. So I formatted them by multiplying by 100, taking the integer and rebuilding a string with the decimal point. This is the result: |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
The image with the code is hard to read - probably not your fault - the forums automatically compresses photo data.... Can you please repost it in a code box? Just makes it easier to read without straining my little eyeballs too much. Smoke makes things work. When the smoke gets out, it stops! |
||||
OA47 Guru Joined: 11/04/2012 Location: AustraliaPosts: 926 |
Sorry about that Grogster. Here's the code in a code box. PrintToScreen:
PVVOLTS=Int(Pin(2)*1208.5)/100 APV$=Str$(Int(PVVOLTS*100)) PV$=Left$(APV$,2)+"."+Right$(APV$,2) SLAVOLTS=Int(Pin(4)*1210)/100 ASV$=Str$(Int(SLAVOLTS*100)) SLA$=Left$(ASV$,2)+"."+Right$(ASV$,2) TEMPC=Pin(3)*264-273 ATC$=Str$(Int(TEMPC*100)) TC$=Left$(ATC$,2)+"."+Right$(ATC$,2) Print Date$;" ";Time$;" | PV ";PV$;" V | SLA ";SLA$;" V | TEMP ";TC$; Print " DEG C | S1";SOL1;" | S2";SOL2 Return |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
Ahhhhhhhh - that's better. My eyeballs thank you for the extra effort. Smoke makes things work. When the smoke gets out, it stops! |
||||
plasma Guru Joined: 08/04/2012 Location: GermanyPosts: 437 |
thx a lot , sometimes i cant see a tree in the forest |
||||
Print this page |