faqts : Computers : Programming : Languages : Tse : Math

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

TSE: Math: Number: Float: Add: How to possibly add very long positive floats?

Jan 23rd, 2004 05:23
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 23 January 2004 - 04:06 am --------------------

TSE: Math: Number: Float: Add: How to possibly add very long positive 
floats?

---

--- cut here ---------------------------------------------------------

FORWARD PROC Main()

FORWARD STRING PROC FNStringGetMathAddFloatTwoS( STRING s1, STRING s2 )

// --- MAIN --- //

PROC Main()

  Warn( FNStringGetMathAddFloatTwoS
( "90.223412333", "1.999987365000000053" ) ) // 
gives "92.223399698000000053"

  Warn( FNStringGetMathAddFloatTwoS
( "12342.223412333", "1234.9999873650053" ) ) // 
gives "13577.2233996980053"

END

<F12> Main()

// --- LIBRARY --- //

// library: string: get: math: add: float: two 
(filenamemacro=getstftw.s) [kn, ri, fr, 23-01-2004  3:54:43]

STRING PROC FNStringGetMathAddFloatTwoS( STRING in1S, STRING in2S )

 // e.g. PROC Main()

 // e.g.   Warn( FNStringGetMathAddFloatTwoS
( "90.223412333", "1.999987365000000053" ) ) // 
gives "92.223399698000000053"

 // e.g.   Warn( FNStringGetMathAddFloatTwoS
( "12342.223412333", "1234.9999873650053" ) ) // 
gives "13577.2233996980053"

 // e.g. END

 // e.g.

 // e.g. <F12> Main()

 INTEGER restI = 0

 INTEGER lengthminI = 0

 INTEGER lengthmaxI = 0

 INTEGER sumI = 0

 INTEGER I = 0

 INTEGER J = 0

 STRING sumS[255] = ""

 STRING minS[255] = ""

 STRING maxS[255] = ""

 STRING cminS[1] = ""

 STRING cmaxS[1] = ""

 STRING integer1S[255] = ""

 STRING integer2S[255] = ""

 STRING decimal1S[255] = ""

 STRING decimal2S[255] = ""

 INTEGER posI = 0

 //

 posI = Pos( ".", in1S )

 IF posI == 0

  integer1S = in1S

  decimal1S = "0"

 ELSE

  integer1S = SubStr( in1S, 1, posI - 1 )

  decimal1S = SubStr( in1S, posI + 1, Length( in1S ) - posI + 1 )

 ENDIF

 posI = Pos( ".", in2S )

 IF posI == 0

  integer2S = in2S

  decimal2S = "0"

 ELSE

  integer2S = SubStr( in2S, 1, posI - 1 )

  decimal2S = SubStr( in2S, posI + 1, Length( in2S ) - posI + 1 )

 ENDIF

 //

 IF Length( decimal1S ) < Length( decimal2S )

  minS = decimal1S

  maxS = decimal2S

 ELSE

  minS = decimal2S

  maxS = decimal1S

 ENDIF

 //

 lengthminI = Length( minS )

 lengthmaxI = Length( maxS )

 //

 FOR I = 1 TO lengthmaxI

  J = lengthmaxI - I + 1

  IF J > 0

   cminS = minS[ J ]

  ELSE

   cminS = "0"

  ENDIF

  cmaxS = maxS[ lengthmaxI - I + 1 ]

  sumI = Val( cmaxS ) + Val( cminS ) + restI

  IF sumI > 9 // AND ( I <> lengthmaxI )

   restI = sumI / ( 9 + 1 )

   sumI = sumI MOD ( 9 + 1 )

  ELSE

   restI = 0

  ENDIF

  sumS = Str( sumI ) + sumS

 ENDFOR

 //

 sumS = "." + sumS

 //

 IF Length( integer1S ) < Length( integer2S )

  minS = integer1S

  maxS = integer2S

 ELSE

  minS = integer2S

  maxS = integer1S

 ENDIF

 lengthminI = Length( minS )

 lengthmaxI = Length( maxS )

 FOR I = 1 TO lengthmaxI

  J = lengthminI - I + 1

  IF J > 0

   cminS = minS[ J ]

  ELSE

   cminS = "0"

  ENDIF

  cmaxS = maxS[ lengthmaxI - I + 1 ]

  sumI = Val( cmaxS ) + Val( cminS ) + restI

  IF sumI > 9 AND ( I <> lengthmaxI )

   restI = sumI / ( 9 + 1 )

   sumI = sumI MOD ( 9 + 1 )

  ELSE

   restI = 0

  ENDIF

  sumS = Str( sumI ) + sumS

 ENDFOR

 //

 RETURN( sumS )

END

--- cut here ---------------------------------------------------------

---
---

Internet: see also:

---

TSE: Math: Number: Integer: Add: How to possibly add very long 
integers?
http://www.faqts.com/knowledge_base/view.phtml/aid/28438/fid/1629

---

TSE: Math: Number: Integer: Multiply: How to possibly multiply very 
long integers?
http://www.faqts.com/knowledge_base/view.phtml/aid/28462/fid/1629

---

----------------------------------------------------------------------
--- Knud van Eeden --- 23 January 2004 - 04:06 am --------------------

TSE: Math: Number: Float: Add: How to possibly add very long positive 
floats?

---

--- cut here ---------------------------------------------------------

see: getstftw.s

--- cut here ---------------------------------------------------------

---
---

Internet: see also:

---

TSE: Math: Number: Operation: Overview: Can you give an overview of 
long digit operations in TSE?
http://www.faqts.com/knowledge_base/view.phtml/aid/28481/fid/1629

----------------------------------------------------------------------

----------------------------------------------------------------------