Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Fixed-point Fmt

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
GTBecker



Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral

Posted: 17 February 2010, 19:23 PM    Post subject: Fixed-point Fmt Reply with quote

Fmt()'s handling of small and large values, replacing a fixed number of fractional digits with exponential notation, is problematic.

To avoid that behavior, I've often converted the value to a fixed-length string integer and inserted the decimal point with Mid(). An attempt to generalize that to a flexible function is:
Code:
function FmtX(byval ValIn as single, byval Digits as byte) as string
   dim str as string
   str = cstr(clng(abs(ValIn) * 10.0^Digits + 10.0^(Digits+1)))
   str = mid(str,2,1) &"."& mid(str,3,Digits)
   if (ValIn < 0.0) and (abs(ValIn) >= 0.5*10.0^-cint(Digits)) then   ' prevent -0.0
      str = "-" & str
   end if
   FmtX = trim(str)
end function

This works fine for absolute values less than 10.0 but isn't generalized sufficiently to work with larger values which, I think, will need a series of integer decade decisions or a log10 to properly place the decimal point. Is there a better way to deal with this?
Back to top
kurakaira



Joined: 21 Mar 2007
Posts: 32
Location: Finland

Posted: 11 December 2010, 15:03 PM    Post subject: FMT() Reply with quote

I haven't had a problem with FMT(LargeNumbers) but with values near zero ... they are a problem .
Since my programs usually follow this formula , Read ADC / other sensors -> Write to display ( using FMT ) , Loop .
My workaround so far has been this ,

if BoostS > (-0.01) and BoostS < 0.01 then
BoostS = 0.0
else
Call WriteLCD
End if

Would it be possible to add a FMTEX() that would not display the scientific notation ?

A new project with a ZX-40r processor ,
www.dlsrevolution.com
The ZX handles gear changing , battery voltages & fuses , amplifier heating & temperatures and door opening ... amongst other little things .
Everything is controlled by a 4.3" touchscreen from Reach Tech .
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 11 December 2010, 16:09 PM    Post subject: Re: FMT() Reply with quote

kurakaira wrote:
Would it be possible to add a FMTEX() that would not display the scientific notation ?
Some time ago, I posted a collection of formatting routines that all generate output to a character array. The FmtFloat() routine will probably do what you need and, if not, could probably be modified slightly to fit your needs. Here is some example code showing how it works with large and small floating point values, using FmtFloat() to implement FmtEx().
Code:
Sub Main()
  Debug.Print FmtEx(1000000.00001, 5)
  Debug.Print FmtEx(1.00001, 5)
  Debug.Print FmtEx(0.00001, 5)
End Sub

Function FmtEx(ByVal f as Single, ByVal decPlaces as Byte) as String
  Dim buf(1 to 20) as Byte
  Dim width as Byte
  Call FmtFloat(f, decPlaces, buf, width)
  FmtEx = MakeString(buf.DataAddress, width)
End Function
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group