atof(“0”) returns 2 in float variable
I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.
I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
val2 = atof(test);
return 0;
}
I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.
UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Could this affect this issue?
As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.
I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?
c embedded stm32f4 atof
|
show 6 more comments
I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.
I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
val2 = atof(test);
return 0;
}
I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.
UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Could this affect this issue?
As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.
I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?
c embedded stm32f4 atof
3
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features ofscanflibrary functions, and may also affectato*andstrto*functions.
– user694733
2 days ago
2
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
1
If what you show is enough for you to replicate the problem for you, then put it into amainfunction, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.
– Some programmer dude
2 days ago
1
Your new code does not use the value returned byatof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.
– Gerhardh
2 days ago
|
show 6 more comments
I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.
I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
val2 = atof(test);
return 0;
}
I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.
UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Could this affect this issue?
As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.
I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?
c embedded stm32f4 atof
I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.
I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
val2 = atof(test);
return 0;
}
I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.
UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Could this affect this issue?
As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.
I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?
c embedded stm32f4 atof
c embedded stm32f4 atof
edited 2 days ago
Clifford
58.6k858125
58.6k858125
asked 2 days ago
BabisIBabisI
517
517
3
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features ofscanflibrary functions, and may also affectato*andstrto*functions.
– user694733
2 days ago
2
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
1
If what you show is enough for you to replicate the problem for you, then put it into amainfunction, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.
– Some programmer dude
2 days ago
1
Your new code does not use the value returned byatof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.
– Gerhardh
2 days ago
|
show 6 more comments
3
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features ofscanflibrary functions, and may also affectato*andstrto*functions.
– user694733
2 days ago
2
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
1
If what you show is enough for you to replicate the problem for you, then put it into amainfunction, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.
– Some programmer dude
2 days ago
1
Your new code does not use the value returned byatof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.
– Gerhardh
2 days ago
3
3
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of
scanf library functions, and may also affect ato* and strto* functions.– user694733
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of
scanf library functions, and may also affect ato* and strto* functions.– user694733
2 days ago
2
2
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
1
1
If what you show is enough for you to replicate the problem for you, then put it into a
main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.– Some programmer dude
2 days ago
If what you show is enough for you to replicate the problem for you, then put it into a
main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.– Some programmer dude
2 days ago
1
1
Your new code does not use the value returned by
atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.– Gerhardh
2 days ago
Your new code does not use the value returned by
atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.– Gerhardh
2 days ago
|
show 6 more comments
2 Answers
2
active
oldest
votes
First, your question lacks a Minimal, Complete, and Verifiable example.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
printf("%fn", val1);
val2 = atof(test);
printf("%fn", val2);
}
Output:
0.000000
0.000000
<°)))<()
Or your standard library implementation is fubar.
add a comment |
It looks like the cause of your problem is in the updated question text:
UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.
See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252773%2fatof0-returns-2-in-float-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, your question lacks a Minimal, Complete, and Verifiable example.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
printf("%fn", val1);
val2 = atof(test);
printf("%fn", val2);
}
Output:
0.000000
0.000000
<°)))<()
Or your standard library implementation is fubar.
add a comment |
First, your question lacks a Minimal, Complete, and Verifiable example.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
printf("%fn", val1);
val2 = atof(test);
printf("%fn", val2);
}
Output:
0.000000
0.000000
<°)))<()
Or your standard library implementation is fubar.
add a comment |
First, your question lacks a Minimal, Complete, and Verifiable example.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
printf("%fn", val1);
val2 = atof(test);
printf("%fn", val2);
}
Output:
0.000000
0.000000
<°)))<()
Or your standard library implementation is fubar.
First, your question lacks a Minimal, Complete, and Verifiable example.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char test = "0";
float val1;
double val2;
val1 = atof(test);
printf("%fn", val1);
val2 = atof(test);
printf("%fn", val2);
}
Output:
0.000000
0.000000
<°)))<()
Or your standard library implementation is fubar.
edited 2 days ago
answered 2 days ago
SwordfishSwordfish
9,19511335
9,19511335
add a comment |
add a comment |
It looks like the cause of your problem is in the updated question text:
UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.
See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.
add a comment |
It looks like the cause of your problem is in the updated question text:
UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.
See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.
add a comment |
It looks like the cause of your problem is in the updated question text:
UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.
See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.
It looks like the cause of your problem is in the updated question text:
UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option
"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".
Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.
See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.
answered 2 days ago
R..R..
155k26258562
155k26258562
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252773%2fatof0-returns-2-in-float-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.
– Some programmer dude
2 days ago
Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of
scanflibrary functions, and may also affectato*andstrto*functions.– user694733
2 days ago
2
The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.
– Yunnosch
2 days ago
1
If what you show is enough for you to replicate the problem for you, then put it into a
mainfunction, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.– Some programmer dude
2 days ago
1
Your new code does not use the value returned by
atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.– Gerhardh
2 days ago