2

When I run this program on my calculator:

void main(void) {
    char *quot = malloc(10 * sizeof(char));
    char *rest = malloc(10 * sizeof(char));
    sprintf(quot, "%d", 5);
    printText(quot, 0, 0);
    sprintf(rest, "%f", 2.03);
    printText(rest, 0, 1);
}

printText function for my TI 84 CE calculator:

void printText(const char *text, uint8_t xpos, uint8_t ypos) {
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}

This is the output on my calculator's LCD:

5
%

There is a percentage token instead of 2.03, what is the reason behind this?

I have included these libraries:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <tice.h> // this is for my TI84
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Tom55555
  • 21
  • 3
  • 2
    _You're running this on a calculator???_ __What???__ Sure thing there will be a lot of bugs and inconsistencies with the Standard. – ForceBru Aug 18 '16 at 15:35
  • 1
    Note: `void main(void){` and lack of `` may be issues, but I suspect the real one is code lacks **explicit** FP math usage yet uses `"%f"`. Insure compilation include the FP math library and/or do some FP math in the code. – chux - Reinstate Monica Aug 18 '16 at 15:39
  • Where (which library) is that sprintf coming from? – Daniel Jour Aug 18 '16 at 15:39
  • 1
    cemetech has an online IDE that supports the C language. Documentation is available on github about the C backend for this project at https://github.com/CE-Programming support for floating point seems present, but may have problems. – chqrlie Aug 18 '16 at 16:13
  • Apparently the language (in particular the standard library implementation) you're working with is not actually C90 but some pseudo-C language the TI84 tools provide. – R.. GitHub STOP HELPING ICE Aug 18 '16 at 16:21
  • Just a side-note: why are you `malloc`'ing `quot` and `res`? It seems to me you could equally well use `char quot[10]` and `char res[10]` – Elias Van Ootegem Aug 18 '16 at 16:36
  • Many implementations where memory is tight do not enable `"%f"` by default. – Weather Vane Aug 18 '16 at 17:51
  • @EliasVanOotegem I used malloc instead of standard array initialisation because of: [link](http://stackoverflow.com/questions/10234288/iso-c90-forbids-variable-length-array) – Tom55555 Aug 18 '16 at 19:11
  • @chqrlie Thanks ! I didn't know that documentation existed ! I am indeed programming in that online IDE – Tom55555 Aug 18 '16 at 19:13
  • 1
    I read the _[link](http://stackoverflow.com/questions/10234288/iso-c90-forbids-variable-length-array)_ you provided a couple of comments up, it seems to be talking about alternatives to VLAs (or the lack of prior to C99). From what you have shown, VLA is not needed. A regular C string (`char quot[10];`) would appear sufficient. (as mentioned in Elias' comment.) – ryyker Aug 18 '16 at 19:35
  • @Tom55555: the link talks about *variable length* arrays, i.e. the size is a variable expression. `char whateverthename[10]` is a fixed length array and is no problem in any C I know of. – Rudy Velthuis Aug 18 '16 at 21:27
  • @Tom55555: the question is *does the embedded version of `sprinf` support floating point conversions?* I could not find an answer on their website, nor could I find the source to that library (but they do mention it to have an LGPL license), You should ask on their forum. – chqrlie Aug 18 '16 at 21:53
  • @Tom55555: that would make sense if you, at some point, were using `realloc`. You're allocating a block of memory to store 10 char's. That can be done on the stack with a fixed length array: `char foo[10]`, that's different to VLA's – Elias Van Ootegem Aug 19 '16 at 07:33

1 Answers1

1

%f is disabled due to memory constraints in the calculator. Float conversion is expensive. The dialect of C used is C89, which is the best C. Anyway, you can enable %f for your program by adding the following line in your makefile:

USE_FLASH_FUNCTIONS := NO

This will greatly increase the size of your binary however, so it is recommended to implement a custom limited version of %f. Alternatively, this code will do the same thing as %f, but copies the output to a character array.

void float2str(float value, char *str) {
    real_t tmp_real = os_FloatToReal(value);
    os_RealToStr(str, &tmp_real, 8, 1, 2);
}
Community
  • 1
  • 1
MateoConLechuga
  • 561
  • 4
  • 23