Author Topic: Weird error: sprintf  (Read 7235 times)

0 Members and 1 Guest are viewing this topic.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Weird error: sprintf
« on: December 24, 2012, 12:11:58 pm »
I was just fiddling with some sine and cosine tables and needed some debugging.
But the output was always "0.00000". I tried to execute
Code: [Select]
float oneandahalf = 1.5f;
double twoandahalf = 2.5;
int one = 1;
char str[60];
sprintf(str, "1.5: %f 2.5: %f 1: %d", oneandahalf, twoandahalf, one);
show_msgbox(str, str);
It compiles fine, result:

Dafuq is going on there :o
Same result with nspire_emu.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Weird error: sprintf
« Reply #1 on: December 24, 2012, 12:40:58 pm »
Lolwut? O.O
I was trying to debug float values too, and was also getting a similar wierd problem D:

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: Weird error: sprintf
« Reply #2 on: December 24, 2012, 12:52:34 pm »
See if this works:

sprintf(str, "1.5: %f 2.5: %f 1: %d", 1.5, 2.5, 1);

It may at the very least narrow down the bug.
« Last Edit: December 24, 2012, 12:53:35 pm by Ranman »
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Weird error: sprintf
« Reply #3 on: December 24, 2012, 01:05:00 pm »
Nope, no change.

Code: [Select]
sprintf(str, "1: %d 2.5: %f 1.5: %f", 1, 2.5f, 1.5);prints: "1: 1 1.5: 0.000000 2.5: 0.000000".
Seems to be some kind of issue with va_list's and aligning, doesn't it?
« Last Edit: December 24, 2012, 01:06:03 pm by Vogtinator »

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: Weird error: sprintf
« Reply #4 on: December 24, 2012, 01:13:19 pm »
Nope, no change.

Code: [Select]
sprintf(str, "1: %d 2.5: %f 1.5: %f", 1, 2.5f, 1.5);prints: "1: 1 1.5: 0.000000 2.5: 0.000000".
Seems to be some kind of issue with va_list's and aligning, doesn't it?

Yeah... looks like something is wrong with sprintf itself or some type of alignment issue.

You should report the bug and give the examples that you posted here.
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Weird error: sprintf
« Reply #5 on: December 24, 2012, 01:20:57 pm »
http://www.unsads.com/projects/nsptools/register
Can't register..
Do I have to mail him?

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Weird error: sprintf
« Reply #6 on: December 24, 2012, 02:37:57 pm »
Maybe GCC and Nucleus / TI's code use different formats for floating-point values ?
(at least, TI's Lua and C do)
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Weird error: sprintf
« Reply #7 on: December 24, 2012, 03:12:55 pm »
You're passing a double to a %f specifier, that's a problem don't you think? Make it %lf for the double.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Weird error: sprintf
« Reply #8 on: December 24, 2012, 04:39:21 pm »
Tried that, doesn't work.
But it shouldn't confuse va_list.

Code: [Select]
sprintf(str, "%d, %f, %lf", 1, 1.5f, 2.5);Should be fine I think.

Quote
Maybe GCC and Nucleus / TI's code use different formats for floating-point values ?
Is sprintf a syscall? Isn't it included in newlib?

Edit:
Gcc thinks "1.5f" is a double..
« Last Edit: December 24, 2012, 05:03:03 pm by Vogtinator »

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Weird error: sprintf
« Reply #9 on: December 24, 2012, 05:17:10 pm »
Quote
Maybe GCC and Nucleus / TI's code use different formats for floating-point values ?
Is sprintf a syscall? Isn't it included in newlib?
sprintf is a syscall, yes. Ndless almost only uses syscalls and AFAIK doesn't use Newlib.
« Last Edit: December 24, 2012, 05:17:45 pm by lkj »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Weird error: sprintf
« Reply #10 on: December 24, 2012, 05:26:53 pm »
Quote
sprintf is a syscall, yes. Ndless almost only uses syscalls and AFAIK doesn't use Newlib.
http://hackspire.unsads.com/wiki/index.php/C_and_assembly_development_introduction_on_Linux
But it gets build and gcc links against it.
« Last Edit: December 24, 2012, 05:27:12 pm by Vogtinator »

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Weird error: sprintf
« Reply #11 on: December 24, 2012, 06:05:21 pm »
http://hackspire.unsads.com/wiki/index.php/Syscalls says Newlib is used for exit() and here it says it's also used for floating point (because there's no FPU). But most other parts of newlib don't work with Ndless.
« Last Edit: December 24, 2012, 06:05:35 pm by lkj »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Weird error: sprintf
« Reply #12 on: December 24, 2012, 06:09:15 pm »
Wouldn't it be better to provide it directly with newlib?
It would be faster and there will be no such problems anymore.
Only disadvantage would be the increasing size of the binary.

Edit: Workaround: Use http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i.
The third answer works perfectly with floats, too.
« Last Edit: December 25, 2012, 08:35:37 am by Vogtinator »