Author Topic: Floatlib  (Read 1061 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • View Profile
Floatlib
« on: May 14, 2016, 06:44:13 pm »
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: 7915
  • Rating: +372/-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

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 243
  • Rating: +14/-0
  • I own two game consoles- a ti- 83 and a ti- 84!
    • 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...
Good things come in bags marked "SWAG"

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • 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

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 243
  • Rating: +14/-0
  • I own two game consoles- a ti- 83 and a ti- 84!
    • 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?)
Good things come in bags marked "SWAG"

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • 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

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 243
  • Rating: +14/-0
  • I own two game consoles- a ti- 83 and a ti- 84!
    • 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)
Good things come in bags marked "SWAG"

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • 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

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 243
  • Rating: +14/-0
  • I own two game consoles- a ti- 83 and a ti- 84!
    • 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?)
Good things come in bags marked "SWAG"

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • 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

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 243
  • Rating: +14/-0
  • I own two game consoles- a ti- 83 and a ti- 84!
    • 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.
Good things come in bags marked "SWAG"

Offline Xeda112358

  • Xombie. I am it.
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4453
  • Rating: +702/-6
  • meow :3
    • 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