Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: Vogtinator on December 24, 2012, 12:11:58 pm

Title: Weird error: sprintf
Post by: Vogtinator 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:
(http://i.imgur.com/wD9lj.png)
Dafuq is going on there :o
Same result with nspire_emu.
Title: Re: Weird error: sprintf
Post by: epic7 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:
Title: Re: Weird error: sprintf
Post by: Ranman 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.
Title: Re: Weird error: sprintf
Post by: Vogtinator 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?
Title: Re: Weird error: sprintf
Post by: Ranman 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.
Title: Re: Weird error: sprintf
Post by: Vogtinator on December 24, 2012, 01:20:57 pm
http://www.unsads.com/projects/nsptools/register (http://www.unsads.com/projects/nsptools/register)
Can't register..
Do I have to mail him?
Title: Re: Weird error: sprintf
Post by: Lionel Debroux 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)
Title: Re: Weird error: sprintf
Post by: calc84maniac 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.
Title: Re: Weird error: sprintf
Post by: Vogtinator 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..
Title: Re: Weird error: sprintf
Post by: lkj 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.
Title: Re: Weird error: sprintf
Post by: Vogtinator 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 (http://hackspire.unsads.com/wiki/index.php/C_and_assembly_development_introduction_on_Linux)
But it gets build and gcc links against it.
Title: Re: Weird error: sprintf
Post by: lkj on December 24, 2012, 06:05:21 pm
http://hackspire.unsads.com/wiki/index.php/Syscalls (http://hackspire.unsads.com/wiki/index.php/Syscalls) says Newlib is used for exit() and here (http://hackspire.unsads.com/wiki/index.php/Ndless_features_and_limitations#Newlib) 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.
Title: Re: Weird error: sprintf
Post by: Vogtinator 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 (http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i).
The third answer works perfectly with floats, too.