# erfc()?

Page 1 of 1

## 12 Replies - 4234 Views - Last Post: 29 October 2010 - 07:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=196541&amp;s=94e6bdc7be610970d34aabe92e77dabc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

# erfc()?

Posted 24 October 2010 - 11:14 AM

Hello everyone!

I was trying to convert the NIST randomness tests to c++. I got this:
```#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include "include\externs.h"
using namespace std;

void Frequency(double n)
{
int		i;
double	f, s_obs, p_value, sum, sqrt2 = 1.41421356237309504880;

sum = 0.0;
for ( i=0; i<n; i++ )
sum += 2*(int)epsilon[i]-1;
s_obs = fabs(sum)/sqrt(n);
f = s_obs/sqrt2;
p_value = erfc(f);

cout << "\t\t\t      FREQUENCY TEST\n";
cout << "\t\t---------------------------------------------\n";
cout << "\t\tCOMPUTATIONAL INFORMATION:\n";
cout << "\t\t---------------------------------------------\n";
cout << "\t\t(a) The nth partial sum = " << (int)sum << "\n";
cout << "\t\t(B)/> S_n/n               = " << sum/n << "\n";
cout << "\t\t---------------------------------------------\n";

string str = p_value < ALPHA ? "FAILURE" : "SUCCESS";

cout << str << "\t\tp_value = " << p_value << "\n\n";
cout << p_value << "\n";
}
```

I don't think I really knew what I was doing, but "erfc()" is not declared. After looking it up, I found it was some kind of error function. Where can I find a c++ implementation?

BTW, here is externs.h (NOTE: I haven't modified any of the header files):
```#include "../include/defs.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L   D A T A  S T R U C T U R E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

extern BitSequence	*epsilon;				// BIT STREAM
extern TP			tp;						// TEST PARAMETER STRUCTURE
extern FILE			*stats[NUMOFTESTS+1];	// FILE OUTPUT STREAM
extern FILE			*results[NUMOFTESTS+1];	// FILE OUTPUT STREAM
extern FILE			*freqfp;				// FILE OUTPUT STREAM
extern FILE			*summary;				// FILE OUTPUT STREAM
extern int			testVector[NUMOFTESTS+1];

extern char	generatorDir[NUMOFGENERATORS][20];
extern char	testNames[NUMOFTESTS+1][32];
```

and here is defs.h:
```/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
D E B U G G I N G  A I D E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "config.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
M A C R O S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define MAX(x,y)             ((x) <  (y)  ? (y)  : (x))
#define MIN(x,y)             ((x) >  (y)  ? (y)  : (x))
#define isNonPositive(x)     ((x) <= 0.e0 ?   1  : 0)
#define isPositive(x)        ((x) >  0.e0 ?   1 : 0)
#define isNegative(x)        ((x) <  0.e0 ?   1 : 0)
#define isGreaterThanOne(x)  ((x) >  1.e0 ?   1 : 0)
#define isZero(x)            ((x) == 0.e0 ?   1 : 0)
#define isOne(x)             ((x) == 1.e0 ?   1 : 0)

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L  C O N S T A N T S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define ALPHA							0.01	/* SIGNIFICANCE LEVEL */
#define MAXNUMOFTEMPLATES				148		/* APERIODIC TEMPLATES: 148=>temp_length=9 */
#define NUMOFTESTS						15		/* MAX TESTS DEFINED  */
#define NUMOFGENERATORS					10		/* MAX PRNGs */
#define MAXFILESPERMITTEDFORPARTITION	148
#define	TEST_FREQUENCY					1
#define	TEST_BLOCK_FREQUENCY			2
#define	TEST_CUSUM						3
#define	TEST_RUNS						4
#define	TEST_LONGEST_RUN				5
#define	TEST_RANK						6
#define	TEST_FFT						7
#define	TEST_NONPERIODIC				8
#define	TEST_OVERLAPPING				9
#define	TEST_UNIVERSAL					10
#define	TEST_APEN						11
#define	TEST_RND_EXCURSION				12
#define	TEST_RND_EXCURSION_VAR			13
#define	TEST_SERIAL						14
#define	TEST_LINEARCOMPLEXITY			15

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L   D A T A  S T R U C T U R E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

typedef unsigned char	BitSequence;

typedef struct _testParameters {
int		n;
int		blockFrequencyBlockLength;
int		nonOverlappingTemplateBlockLength;
int		overlappingTemplateBlockLength;
int		serialBlockLength;
int		linearComplexitySequenceLength;
int		approximateEntropyBlockLength;
int		numOfBitStreams;
} TP;
```

AND here is config.h:
```#if defined(__cplusplus)
extern "C" {
#endif

#ifndef _CONFIG_H_
#define	_CONFIG_H_

//#define	WINDOWS32
//#define	PROTOTYPES
//#define	LITTLE_ENDIAN
//#define	LOWHI

/*
* AUTO DEFINES (DON'T TOUCH!)
*/

#ifndef	CSTRTD
typedef char *CSTRTD;
#endif
#ifndef	BSTRTD
typedef unsigned char *BSTRTD;
#endif

#ifndef	BYTE
typedef unsigned char BYTE;
#endif
#ifndef	UINT
typedef unsigned int UINT;
#endif
#ifndef	USHORT
typedef unsigned short USHORT;
#endif
#ifndef	ULONG
typedef unsigned long ULONG;
#endif
#ifndef	DIGIT
typedef USHORT DIGIT;	/* 16-bit word */
#endif
#ifndef	DBLWORD
typedef ULONG DBLWORD;  /* 32-bit word */
#endif

#ifndef	WORD64
typedef ULONG WORD64[2];  /* 64-bit word */
#endif

#endif /* _CONFIG_H_ */

#if defined(__cplusplus)
}
#endif
```

Thanks,
GEOFBOT

Is This A Good Question/Topic? 0

## Replies To: erfc()?

### #2 janotte

• code > sword

Reputation: 988
• Posts: 5,135
• Joined: 28-September 06

## Re: erfc()?

Posted 25 October 2010 - 12:03 AM

Here is what erfc() is complementary error function
http://en.wikipedia..../Error_function

So in a total of about a quarter of the time on Google that it probably took you to post all this the answer is here:
http://en.wikipedia....h#C99_functions

### #3 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

## Re: erfc()?

Posted 25 October 2010 - 02:31 PM

Hmmm, I take a look at that! Thanks!

GEOFBOT

Edit: I don't think erfc() is included with the new c math library included in visual c++ express (neither cmath or math.h). I'll have to find an external source.

This post has been edited by geofbot: 25 October 2010 - 02:32 PM

### #4 janotte

• code > sword

Reputation: 988
• Posts: 5,135
• Joined: 28-September 06

## Re: erfc()?

Posted 25 October 2010 - 09:08 PM

I suppose anything is possible in this big world but that seems highly unlikely.

Show us a "Hello world" C++ program where you include cmath and show us the error message you get when you try to compile it.

### #5 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

## Re: erfc()?

Posted 26 October 2010 - 03:29 PM

Sorry if that was a little confusing, but what I meant was that VC++ has the cmath header file, but erfc() is not included. Guess I worded that badly.

### #6 janotte

• code > sword

Reputation: 988
• Posts: 5,135
• Joined: 28-September 06

## Re: erfc()?

Posted 26 October 2010 - 03:44 PM

How interesting. Are they using a pre-C99 compiler maybe? We may have discovered something quite interesting here.
Just so we are on the same page. I wrote this
```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double theErfc = erfc(4.6);
cout << "theErfc = " << theErfc << endl;
return 0;
}

```

which compiles under G++ 4.2 and produces this output:
```theErfc = 7.7496e-11

```

EDIT - If anyone else would like to run the experiment and tell us if their setup fails to compile and/or run this code that would be spiffing . Obviously if you do get a 'fail' please let us know the setup it failed on.

This post has been edited by janotte: 26 October 2010 - 03:52 PM

### #7 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

## Re: erfc()?

Posted 28 October 2010 - 01:36 PM

It was a fail.
Here is the complete compiler output:
```1>------ Build started: Project: cmathTest, Configuration: Debug Win32 ------
1>  main.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
```

In the code editor, there is a little red line ( like what you get from spellcheck in MS Word or openoffice) under erfc(). When putting your cursor over it, it reads:
Error: identifier "erfc" is undefined.

BTW, here is the console output in the Visual Stido 2010 Command Prompt after typing in 'cl' (the compiler):
```Setting environment for using Microsoft Visual Studio 2010 x86 tools.

C:\Program Files\Microsoft Visual Studio 10.0\VC>cl
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86

C:\Program Files\Microsoft Visual Studio 10.0\VC>
```

### #8 Munawwar

• D.I.C Regular

Reputation: 161
• Posts: 457
• Joined: 20-January 10

## Re: erfc()?

Posted 28 October 2010 - 01:56 PM

erfc isn't in cmath in VC++. Check this link.
As said in the link, alternative is to use boost library.

### #9 janotte

• code > sword

Reputation: 988
• Posts: 5,135
• Joined: 28-September 06

## Re: erfc()?

Posted 28 October 2010 - 02:54 PM

How very interesting.

What is it with MS and standards? Why do they so often fail to stick to them? So strange. It's a 10 year old standard after all.

That's assuming, of course, that erfc() is really part of C99 as what I found said it is. I haven't looked at the official documents. Can anyone confirm or deny?

Sorry I couldn't help and thanks for educating me.

### #10 Munawwar

• D.I.C Regular

Reputation: 161
• Posts: 457
• Joined: 20-January 10

## Re: erfc()?

Posted 28 October 2010 - 03:11 PM

From open-std.org, I see it is part of the C99 standard. No idea why MS hasn't updated.

### #11 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

## Re: erfc()?

Posted 28 October 2010 - 03:59 PM

No problem. And Munawwar, I'll be sure to try the boost libraries. I've used them before for "lexical_cast" or somthing. Thanks!

### #12 janotte

• code > sword

Reputation: 988
• Posts: 5,135
• Joined: 28-September 06

## Re: erfc()?

Posted 29 October 2010 - 01:45 AM

There are a couple of other obvious options to consider to get around MS's failure to keep up with the standards.

Option 1

Option 2

### #13 geofbot

Reputation: 24
• Posts: 80
• Joined: 26-September 10

## Re: erfc()?

Posted 29 October 2010 - 07:18 AM

Yeah.
I have to admit, I've been turning to the dark side (heh heh, I mean closed source software).