Author Topic: Real --> integer_part + decimal_part  (Read 4447 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Real --> integer_part + decimal_part
« on: September 23, 2021, 05:48:57 pm »
Looking for best way to do ...

I will process the integer_part and decimal_part differently




  [ You are not allowed to view this attachment ]  

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #1 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?

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #2 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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Real --> integer_part + decimal_part
« Reply #3 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?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #4 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))

FellippeHeitor

  • Guest
Re: Real --> integer_part + decimal_part
« Reply #5 on: September 23, 2021, 07:47:05 pm »
Try Fix()

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #6 on: September 23, 2021, 07:52:54 pm »
right, Fix will work

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #7 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




  [ You are not allowed to view this attachment ]  



Marked as best answer by Richard on September 24, 2021, 06:58:19 am

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #8 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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Real --> integer_part + decimal_part
« Reply #9 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)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #10 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()

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #11 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.  

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #12 on: September 24, 2021, 10:56:30 am »
@FellippeHeitor @SMcNeill @jack  Many thanks



  [ You are not allowed to view this attachment ]  



@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.


Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Real --> integer_part + decimal_part
« Reply #13 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.