473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

_matherr doesn't get invoked when sscanf is called.

Hi,

As per the msdn knowledge base.. i find the following

"In an application developed with Microsoft C or C/C++, the sscanf()
function is a good alternative to the atof() function to convert a string of
digits into a floating-point number. If a string does not represent a valid
number, atof() returns the value zero; sscanf() returns more useful error
information. The application can use the error value from sscanf() with the
matherr() function to perform error handling. The atof() function does not
call matherr() unless an actual math exception occurs. "

But even if the string is not correctly converted to float the _matherr()
function is not being called. Is this a bug or something wrong?

float f;

char temp[]="1mmm1";

int i=sscanf(temp,"%g",&f);

Here f get assigned with 1 instead i was looking for an error.

Any ideas how to solve this

Thanks

ishekara



Nov 14 '05 #1
4 1876
In 'comp.lang.c', "ishekara" <is******@ishekara.com> wrote:
As per the msdn knowledge base.. i find the following
Huh! Why not reading a good C-book in the first place?

www.accu.org
"In an application developed with Microsoft C or C/C++, the sscanf()
function is a good alternative to the atof() function to convert a
string of digits into a floating-point number. If a string does not
represent a valid number, atof() returns the value zero; sscanf()
returns more useful error information. The application can use the error
value from sscanf() with the matherr() function to perform error
handling. The atof() function does not call matherr() unless an actual
math exception occurs. "

But even if the string is not correctly converted to float the
_matherr() function is not being called. Is this a bug or something
wrong?

float f;

char temp[]="1mmm1";

int i=sscanf(temp,"%g",&f);

Here f get assigned with 1 instead i was looking for an error.
It's not really an error. But you could pre-check the domain with

{
char checked[128];
int ret = sscanf (checked, "%[0-9-+.eE]s", temp);

if (ret == 1)
{
int i = sscanf (checked, "%g", &f);
<...>
}
}

but it wont prevent against malformed expressions like

"12+3" or "45e"
Any ideas how to solve this


Yes. Better to use strtod() and to learn how to use the 2nd parameter. It's
worthy.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2
Thanks, strtod works for me but is it good to use it for float?
"Emmanuel Delahaye" <em**********@noos.fr> wrote in message
news:Xn***************************@212.27.42.73...
In 'comp.lang.c', "ishekara" <is******@ishekara.com> wrote:
As per the msdn knowledge base.. i find the following
Huh! Why not reading a good C-book in the first place?

www.accu.org
"In an application developed with Microsoft C or C/C++, the sscanf()
function is a good alternative to the atof() function to convert a
string of digits into a floating-point number. If a string does not
represent a valid number, atof() returns the value zero; sscanf()
returns more useful error information. The application can use the error
value from sscanf() with the matherr() function to perform error
handling. The atof() function does not call matherr() unless an actual
math exception occurs. "

But even if the string is not correctly converted to float the
_matherr() function is not being called. Is this a bug or something
wrong?

float f;

char temp[]="1mmm1";

int i=sscanf(temp,"%g",&f);

Here f get assigned with 1 instead i was looking for an error.


It's not really an error. But you could pre-check the domain with

{
char checked[128];
int ret = sscanf (checked, "%[0-9-+.eE]s", temp);

if (ret == 1)
{
int i = sscanf (checked, "%g", &f);
<...>
}
}

but it wont prevent against malformed expressions like

"12+3" or "45e"
Any ideas how to solve this


Yes. Better to use strtod() and to learn how to use the 2nd parameter.

It's worthy.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Nov 14 '05 #3
In 'comp.lang.c', "ishekara" <is******@ishekara.com> wrote:
Thanks, strtod works for me but is it good to use it for float?


Yes, it is.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4
Emmanuel

wrote:
In 'comp.lang.c', "ishekara" <is******@ishekara.com> wrote:

Thanks, strtod works for me but is it good to use it for float?

Yes, it is.


Well, no: it's only good for *some* `float' values.
strtod() will happily and successfully convert a string
to a `double' that can't in turn be converted to `float'.
For example, on many implementations the string "1E300"
is acceptable to strtod(), but yields a value larger
than FLT_MAX.

C99 introduced strtof(), which might be available on
ishekara's implementation (it's worth looking, anyhow).
If it isn't, I think the best he can do is to use strtod()
with the usual error checks, and then either hope for the
best or add further checks for range and perhaps precision:

float flt;
double dbl = strtod(...);
/* usual checks here */
if (fabs(dbl) > FLT_MAX
|| (fabs(dbl) < FLT_MIN && dbl != 0.0)) {
/* `double' value outside `float' range */
}
else {
flt = dbl;
}

.... but on systems that support infinities and/or NaNs,
even this much isn't quite enough.

--
Er*********@sun.com

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Charles Jamieson | last post by:
I declare a class class myClass{ public: ~myClass();
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
6
by: Martin | last post by:
Hi, Since I went ASP.NET with my global.asa (making it a global.asax) the application events are called just fine as well as the Session_OnStart event but the Session_OnEnd event is not. What is...
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
185
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
5
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
2
by: Andy Fish | last post by:
hi, i have a class called foo that has a method called ~foo (well it might not be a method but I'm not worried about the actual semantics here) from what I can tell this makes it a destructor...
6
by: chang | last post by:
Hi ALL, I am working in C from past few months. Still now i can't figure out who is called main() in 'C' programme? Main() is a function from that we can call our sunroutines but someone has to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.