QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Richard on September 23, 2021, 05:48:57 pm

Title: Real --> integer_part + decimal_part
Post by: Richard on September 23, 2021, 05:48:57 pm
Looking for best way to do ...

I will process the integer_part and decimal_part differently




  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 23, 2021, 06:31:15 pm
Richard, I would try Int to get the integer part, then for the fractional part simply subtract the integer part
you need to be aware that Int behaves like C floor so for example
Code: QB64: [Select]
  1.  
  2. y = -3141592653589.7932384626433832795##
  3. k = Int(y)
  4. Print k ' -3141592653590
  5.  
you could also use C runtime functions like trunc, trunc will simply truncate the number so if you used trunc in the above example you would get -3141592653589
what range and precision are the values you want to work with?
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 23, 2021, 06:41:18 pm
for example
Code: QB64: [Select]
  1.     Function trunc# (ByVal x As Double)
  2.  
  3.  
  4. y = -3141592653589.7932384626433832795#
  5. k = trunc(y)
  6. Print k, y - k
  7.  
Title: Re: Real --> integer_part + decimal_part
Post by: SMcNeill on September 23, 2021, 07:12:47 pm
Why do you need a declare library for that, jack?

k = INT(y)

Why doesn’t the above work?  Am I missing something here?
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 23, 2021, 07:25:20 pm
@SMcNeill you apparently didn't read my post carefully, Int(-3.5) gives -4, Trunc(-3.5) gives -3
you could of course work around Int's behavior maybe sgn(x)*int(abs(x))
Title: Re: Real --> integer_part + decimal_part
Post by: FellippeHeitor on September 23, 2021, 07:47:05 pm
Try Fix()
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 23, 2021, 07:52:54 pm
right, Fix will work
Title: Re: Real --> integer_part + decimal_part
Post by: Richard on September 24, 2021, 06:03:13 am
I still have trouble with the decimal_part.

@jack the "displayed precision" may only require LONG (INTEGER may be a bit too small) and anticipated only 3 decimal values (at most 4) but the REAL NUMBER may be FLOAT




  [ This attachment cannot be displayed inline in 'Print Page' view ]  


Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 24, 2021, 08:11:04 am
@jack ... but the REAL NUMBER may be FLOAT
in that case don't use _Float, if you use Double then you should get the expected result, as it seems there's a bug in Print Using with _Float
edit, sorry I took FLOAT as meaning Single, for _Float you would need to work around the bug
Title: Re: Real --> integer_part + decimal_part
Post by: SMcNeill on September 24, 2021, 08:13:56 am
Have you tried simply making it a string and just separating it at the decimal?

s$ = STR(x#)
decimal = INSTR(s$, “.”
leftside = LEFT$(s$, decimal)
rightside = MID$(s$, decimal +1)
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 24, 2021, 09:42:53 am
something anyone that uses the type _Float should be aware of is that only basic arithmetic operators are supported, that is +, -, *,  / and <, =, >, <>
not even Abs() is supported the functions return double precision result, except for Exp()
Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 24, 2021, 10:19:13 am
@Richard
may I suggest something like this
Code: Text: [Select]
  1. Option _Explicit
  2. Declare Library
  3.     Function snprintf& (Dest As String, Byval l As Long, frmt As String, Byval x As _Float)
  4. End Declare
  5.  
  6. Dim As _Float c
  7.  
  8. c = 123.45644444444444##
  9. Print strf(c)
  10. Function strf$ (x As _Float)
  11.     Dim As String s
  12.     Dim As String frmt
  13.     Dim As Long l, sign
  14.     sign = Sgn(x)
  15.     If sign < 0 Then x = -x
  16.     s = Spc(64)
  17.     frmt = "%19.3Lf" + Chr$(0)
  18.     l = snprintf(s, Len(s), frmt, x)
  19.     s = _Trim$(s)
  20.     If sign < 0 Then s = "-" + s Else s = " " + s
  21.     strf = s
  22. End Function
  23.  
Title: Re: Real --> integer_part + decimal_part
Post by: Richard on September 24, 2021, 10:56:30 am
@FellippeHeitor @SMcNeill @jack  Many thanks



  [ This attachment cannot be displayed inline in 'Print Page' view ]  



@jack I will try out your last reply soon - it was only the displaying on screen what I was trying to sort out. Thanks for the IMPORTANT REMINDER re where _FLOAT precision is maintained.

Title: Re: Real --> integer_part + decimal_part
Post by: jack on September 24, 2021, 11:43:09 am
Richard, if you need special functions evaluated to _Float precision you can call the C runtime functions https://en.cppreference.com/w/c/numeric/math
for example
Code: QB64: [Select]
  1.     Function snprintf& (Dest As String, Byval l As Long, frmt As String, Byval x As _Float)
  2.     Function logl## (ByVal x As _Float)
  3.     Function expl## (ByVal x As _Float)
  4.     Function sinl## (ByVal x As _Float)
  5.     Function cosl## (ByVal x As _Float)
  6.     Function tanl## (ByVal x As _Float)
  7.     Function asinl## (ByVal x As _Float)
  8.     Function acosl## (ByVal x As _Float)
  9.     Function atanl## (ByVal x As _Float)
  10.     Function tgammal## (ByVal x As _Float)
  11.     Function truncl## (ByVal x As _Float)
  12.     Function fabsl## (ByVal x As _Float)
  13.