Author Topic: Floatlib  (Read 17565 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Floatlib
« on: May 14, 2016, 06:44:13 pm »
EDIT 14 January 2019 : I put this project up on GitHub (Floatlib), incorporating the z80float library that I have been updating and fixing and adding to. Floatlib now has extended-precision support as well as a more complete library of single-precision float routines!

I made a cute little app for my calc that I thought I would share! It basically puts all of my single-precision floats into one app and provides a somewhat easy way to access the routines through a jump table.

It even features an in-app reference for the routines which is useful for when I'm programming on my calc, as well as a hexadecimal translation of the header just in case I forget the code.

I included two examples. The first simply computes and displays eπ. The second uses an algorithm similar to that of the Arithmetic-Geometric Mean algorithm to compute arctangent. It takes the input number as a string in Ans.

Warning: I've run into a few bugs (some are documented, others I haven't chased down yet). Pro-tip: Don't store your program variables in the range of scrap to about scrap+30. The default value for scrap is 8000h. If you change this, please note that the app expects the scrap region to be 256 bytes.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Floatlib
« Reply #1 on: May 15, 2016, 05:02:40 am »
That is looking quite awesome, great work as usual xeda! ^.^

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Floatlib
« Reply #2 on: May 17, 2016, 04:14:12 pm »
Is there any way I can use this in Axe?

*E37 totally ins't thinking about making a quadform...
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #3 on: May 17, 2016, 04:20:38 pm »
It can definitely work with Axe, but you would need to do a lot of the coding in assembly. That said, the app has a built-in reference sheet for the necessary header and addresses of the routines. I'm not familiar with how to make axioms, unfortunately.

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Floatlib
« Reply #4 on: May 17, 2016, 04:31:14 pm »
Woah! that is a little above my head! I only know the basics of assembly and I use Mimas so I would have to transfer every file to view it. I have seen several tutorials for axioms before and it doesn't look that hard. Anyway... it is really cool!
(How long did it take you to learn asm?)
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #5 on: May 17, 2016, 04:46:26 pm »
I started to figure out assembly after about 1.5 years of programming my calculator.
I will see if I can do anything with Mimas to add support for Floatlib. I didn't know it was updated (I have an older version).

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Floatlib
« Reply #6 on: May 17, 2016, 04:54:22 pm »
I started programming my calc half way through last year.
Is there any way to convert a compiled program into hex? (mimas to axe) or compiled to mimas (axe to mimas)
I want to decompile Axe to help learn asm. which is the simplest routine in floatlib? (where to start reading the code and maybe learn something from it)
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #7 on: May 17, 2016, 05:21:51 pm »
When you are learning assembly, it comes in layers. Trying to understand a code that offers high-level functionality is going to take experience.

I suggest you play with it first. Check out the first few codes here and try them out on your calculator. To help a bit, the Invert code can be broken down as:


  AsmPrgm210000115F3FEF5F4DC9
=>AsmPrgm210000115F3FEF5F4DC9

The stuff in RED is an X-coordinate (the first one must NOT be bigger than the second, else it will crash).
The stuff in BLUE is a Y-coordinate (the first one must NOT be bigger than the second, else it will crash).

In source code format, this would be:

    ld hl,$0000
    ld de,$3F5F
    bcall(_InvertRect)
    ret

Alternatively, you could do the following, which is equivalent, but larger and slightly slower:

    ld h,$00
    ld l,$00
    ld d,$3F
    ld e,5F
    bcall(_InvertRect)
    ret

Which in hexadecimal is:

AsmPrgm26002E0016001E00EF5F4DC9

You can swap the order of the loads (so like ld de,$3F5F \ ld hl,0) but those all need to be done before calling the system routine as these are its arguments.

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Floatlib
« Reply #8 on: May 17, 2016, 06:49:40 pm »
Is there any program to turn hex codes to asm code?
The program works! (what is the address of the screen?)
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #9 on: May 17, 2016, 07:17:44 pm »
Is there any program to turn hex codes to asm code?
It is called a disassembler, and I'm sure there are, but I don't know any off the top of my head. If possible, I suggest looking directly at the source code (usually included with any program). A disassembler can only give generic label names, can't preserve macros, and it will convert all of the data to code, too, which gets pretty confusing for somebody with experience, let alone without it.

The program works! (what is the address of the screen?)
I'm not quite sure what you mean. The OS uses plotSScreen to store a copy of the graph screen data. If you do something like this, you might see what is going on:
Code: [Select]
    ld hl,plotSScreen
    ld a,%01010101
    ld b,3
loop:
    ld (hl),a
    inc hl
    djnz loop
    bcall(_GrBufCpy)
    ret


As a side note, I couldn't figure out the format for Mimas header files, so I've been manually typing in the data in Mimas. It's slow going. For now, if you have Floatlib on your calc, try this Mimas program:
Code: [Select]
    ORG userMem-2
    DB $BB,$6D
Start:
    ld hl,appname
    rst rMov9ToOP1
    bcall FindApp
    ret c
    ld b,a
    in a,(6)
    push af
    ld a,b
    out (6),a
    call START
    pop af
    out (6),a
    ret
appname:
    DB 14H
    DB "Floatlib"
START:
iconstSingle=4024H
addSingle=402AH
single2str=407EH
scrap=8000H
var=scrap+30
_pi=0
_e=1
    call iconstSingle
    DB _pi
    ex de,hl
    DB _e
    ld bc,var
    call addSingle
    ld h,b
    ld l,c
    ld bc,scrap
    call single2str
    ld h,b
    ld l,c
    bcall PutS
    ret
It's a lot, but it's mostly just the header, then the actual code. If you do happen to get that code working, you can try other constants instead of _e and _pi (listed in the docs/floatlib.inc file in the Floatlib download) as well as different operations than just addSingle. They are also all listed in the Floatlib App (just run it on the calc and go into the various menus), but that could get annoying switching back and forth between apps.

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Floatlib
« Reply #10 on: May 17, 2016, 08:08:30 pm »
I have been poking around at making an axiom. Axe recognizes it as a valid axiom but won't replace tokens or use the command.
My calc threw a fit today and refused to accept any files choosing rather to freeze and make me pull the batteries. (I tried several times)
It will probably work tomorrow.
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #11 on: August 02, 2018, 01:23:30 am »
I updated the routine for converting a float to a string. Overall, the new code is 14 bytes larger, but faster, a lot more accurate, and less buggy (It actually hasn't crashed on any of the inputs I've used!)
The only weird thing is that it can return "-NaN". The value which causes this is currently invalid and will not be returned by any of the other routines, so you have to manually create the number. In the future, I might make "-NaN" represent a second kind of NaN.

Interestingly, of all of the routines, single2str is the largest at 576 bytes. The next runner-up is the routine for 2^x, which includes all of the core routines for x^y, e^x, and 10^x as well, at 467 bytes.

In the screenshot, I am running the program ATANAGM. It takes a string as input, then prints the result with up to 7 digits. It uses an AGM-like algorithm to compute the arctangent, so I'm honestly surprised at the accuracy. Normally AGM is run in double the desired precision as error accumulates, whereas my code is just using all single precision operations.

New examples:
ftos.8xp just displays the results of e^pi, e-pi, e/pi, and e*pi.
mset.8xp draws the Mandelbrot Set.

Spoiler For New Float -> String Algorithm:
This is an idea that I have had for a loooong time that I finally got up the nerve to implement. In fact, this idea was one of the first candidates for my float library. It was daunting,but well worth it and easy to generalize to larger precisions ;)

I want to display ##x## as a base-10 string. My starting goal is to get it into a format like ##x=10^{n}y##, where ##n## is an integer. I'm not going to be too picky about ##n##, so long as ##y## is smallish in magnitude. For example, if ##y\in[.1,10]##, then it is a lot easier to convert ##y## to a string and append an e##n## to that string. It would be like a pseudo scientific notation. From there, I can work on the formatting and making it look pretty.

So what I do first, is get an approximate value for ##n##. My floats are already in a format of ##x=2^{e}\cdot m##, so ##n\approx e\cdot log_{10}2##. Since ##e## is an 8-bit signed integer in this case, and we are only looking for an integer result, we can approximate this further by getting ##log_{10}2## to 8 bits of precision. Finally, ##n\approx \lfloor \frac{77e}{256}\rfloor##. In assembly, this translates to "Multiply by 77 and take the overflow."

Now that I have ##n##, I want to compute ##y## This is done as ##y=\frac{x}{10^{n}}##. Luckily, this sort of exponentiation is easy. For example, ##10^{13}=10^{1+4+8}=10^{1}\cdot 10^{4}\cdot 10^{8}##. In this case, ##0\le n\le 38##, so if we precompute a table for ##10^{1},10^{2},10^{4},10^{8},10^{16},10^{32}##, then we will only ever need at most 5 multiplications to compute ##10^{n}##. With this, we compute ##y##, which should be on ##y\in [.5,20]##. From there, we proceed to convert y to a string and perform any fancy formatting our hearts desire.


Applying this algorithm to the 80-bit floats will make it a lot faster to convert. It won't be as fast as converting a BCD float like TI's, but it will be more on the order of 40,000cc instead of 400,000cc

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #12 on: January 14, 2019, 11:22:26 pm »
I put this project up on GitHub (Floatlib), incorporating the z80float library that I have been updating and fixing and adding to. Floatlib now has extended-precision support as well as a more complete library of single-precision float routines! The only problem is that when I went to test the library with the the Mandelbrot Set program, it appeared that there are some new issues that need fixing :|


Those two white pixels at/near the center of the circular and cardioid part of the set should be black.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #13 on: February 19, 2019, 12:55:54 am »
Some more routines were finally added in, especially the string -> float extended-precision routine. I still need to implement some if the trig routines, but I only have 565 bytes of space left :| I might need to look into ways to reduce the size of the built-in documentation. Here is the compiled app, the source is up on GitHub.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Floatlib
« Reply #14 on: March 26, 2019, 06:57:36 pm »
I am down to 3 bytes left in the app!

Thankfully it is just about finished. I broke some compatibility, but I don't think people are really using those parts yet. In particular, I removed 5 float constants used in constSingle, and I changed constSingle. The readme and in-app description differed on usage and I changed it to be that described in-app. So before, you would get a pointer to a float with ID 0 (pi) by doing:
Code: [Select]
  call iconstSingle \ .db 0
But the in-app description says the arg is passed in A. So now it is:
Code: [Select]
  ld a,0
  call constSingle

Since this uses the z80float library, most of the other changes were inherited from that. This includes many bug fixes and optimizations (most notably the extended-precision square root routine is much smaller and a little faster and divSingle detects underflow and overflow better). Finally, sinSingle and cosSingle apply range reduction.

And then there were the routines that were added:
Code: [Select]
  xmod1       basically gets the non-integer part of the float
  mod1Single  basically gets the non-integer part of the float
  xconst      the extended-precision variant of constSingle
  ti2single   converts a TI float to a single-precision float
  TItox       converts a TI float to an extended-precision float
  xtoTI       converts an extended-precision float to a TI float
  xcosh       extended-precision cosh
  xcos        extended-precision cos
  xsinh       extended-precision sinh
  xsin        extended-precision sin
  xtanh       extended-precision tanh
  xtan        extended-precision tan

And with that, I think I've included all of the routines that I originally intended! Now I just have to work on tidying up and finding and fixing bugs.

EDIT: Updated readme in the attachment