MISRA-C diary(C言語日誌)

2013年12月の記事一覧

C2011 K.3.9.2.3.1 The wcstok_s function // Example

// filename:c2011-K-3-9-2-3-1-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.9.2.3.1 The wcstok_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <wchar.h>

int main(void)
{
static wchar_t str1[] = L"?a???b,,,#c";
static wchar_t str2[] = L"\t \t";
wchar_t *t, *ptr1, *ptr2;
rsize_t max1 = wcslen(str1)+1;
rsize_t max2 = wcslen(str2)+1;
t = wcstok_s(str1, &max1, "?", &ptr1); // t points to the token "a"
wprintf(L"%ls",t);
t = wcstok_s(NULL, &max1, ",", &ptr1); // t points to the token "??b"
wprintf(L"%ls",t);
t = wcstok_s(str2, &max2, " \t", &ptr2); // t is a null pointer
wprintf(L"%ls",t);
t = wcstok_s(NULL, &max1, "#,", &ptr1); // t points to the token "c"
wprintf(L"%ls",t);
t = wcstok_s(NULL, &max1, "?", &ptr1); // t is a null pointer
wprintf(L"%ls",t);
return printf("K.3.9.2.3.1 The wcstok_s function  \n" );
}
// output may be
------------------------
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
#include "stdafx.h"
// filename:c2011-K-3-9-2-3-1-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.9.2.3.1 The wcstok_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <wchar.h>
int main(void)
//int _tmain(int argc, _TCHAR* argv[])
{
 static wchar_t str1[] = L"?a???b,,,#c";
 static wchar_t str2[] = L"\t \t";
 wchar_t *t, *ptr1, *ptr2;
 rsize_t max1 = wcslen(str1) + 1;
 rsize_t max2 = wcslen(str2) + 1;
 t = wcstok_s(str1, /*&max1, */ L"?", &ptr1); // t points to the token "a"
 wprintf(L"%ls", t);
 t = wcstok_s(NULL, /*&max1,*/ L",", &ptr1); // t points to the token "??b"
 wprintf(L"%ls", t);
 t = wcstok_s(str2, /*&max2,*/ L" \t", &ptr2); // t is a null pointer
 wprintf(L"%ls", t);
 t = wcstok_s(NULL, /*&max1,*/ L"#,", &ptr1); // t points to the token "c"
 wprintf(L"%ls", t);
 t = wcstok_s(NULL,/* &max1,*/ L"?", &ptr1); // t is a null pointer
 wprintf(L"%ls", t);
 return printf("K.3.9.2.3.1 The wcstok_s function  \n");
}
// error
// エラー 1 error C2664 : 'wchar_t *wcstok_s(wchar_t *,const wchar_t *,wchar_t **)' : 引数 2 を 'const //char [2]' から 'const wchar_t *' へ変換できません。 c : \users\documents\visual studio 2013\projects \consoleapplication1\consoleapplication1\consoleapplication1.cpp 53 1 ConsoleApplication1
//'ConsoleApplication1.exe' (Win32) : 'C:\Users\小川 清\Documents\Visual Studio 2013\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe' が読み込まれました。シンボルが読み込まれました。
//'ConsoleApplication1.exe' (Win32) : 'C:\Windows\System32\ntdll.dll' が読み込まれました。PDB ファイルを開けないか、ファイルが見つかりません。
//'ConsoleApplication1.exe' (Win32) : 'C:\Windows\System32\kernel32.dll' が読み込まれました。PDB ファイルを開けないか、ファイルが見つかりません。
//'ConsoleApplication1.exe' (Win32) : 'C:\Windows\System32\KernelBase.dll' が読み込まれました。PDB ファイルを開けないか、ファイルが見つかりません。
//'ConsoleApplication1.exe' (Win32) : 'C:\Windows\System32\msvcr120d.dll' が読み込まれました。PDB ファイルを開けないか、ファイルが見つかりません。
// output may be
// a??b(null)c(null)K.3.9.2.3.1 The wcstok_s function

<この稿は書きかけです。順次追記しています。>


twitter:@kaizen_nagoya
0

C2011 K.3.9.2.2.2 The wcsncat_s function // Example

// filename:c2011-K-3-9-2-2-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.9.2.2.2 The wcsncat_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <wchar.h>

int main(void)
{

/* ... */
wchar_t s1[100] = L"good";
wchar_t s2[6] = L"hello";
wchar_t s3[6] = L"hello";
wchar_t s4[7] = L"abc";
wchar_t s5[1000] = L"bye";
int r1, r2, r3, r4;
r1 = wcsncat_s(s1, 100, s5, 1000);
r2 = wcsncat_s(s2, 6, L"", 1);
r3 = wcsncat_s(s3, 6, L"X", 2);
r4 = wcsncat_s(s4, 7, L"defghijklmn", 3);
return printf("K.3.9.2.2.2 The wcsncat_s function %d %d %d %d \n",r1,r2,r3,r4 );
}
// output may be
-----------------------
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include "stdafx.h"
// filename:c2011-K-3-9-2-2-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.9.2.2.2 The wcsncat_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <wchar.h>
int main(void)
{
 /* ... */
 wchar_t s1[100] = L"good";
 wchar_t s2[16] = L"hello";
 wchar_t s3[16] = L"hello";
 wchar_t s4[17] = L"abc";
 wchar_t s5[10] = L"bye";
 int r1, r2, r3, r4;
 r1 = wcsncat_s(s1, 100, s5, 10);
 r2 = wcsncat_s(s2, 6, L"", 1);
 r3 = wcsncat_s(s3, 6, L"X", 2);
 r4 = wcsncat_s(s4, 7, L"defghijklmn", 3);
 return printf("K.3.9.2.2.2 The wcsncat_s function %d %d %d %d \n", r1, r2, r3, r4);
}

Debug Assertion Failed!
File: f:dd:vctools\crtw32\h\tcsncat_s.ini
Line:72
Expression("Buffer is too small" && 0)
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 K.3.9.2.1.2 The wcsncpy_s function // Example

// filename:c2011-K-3-9-2-1-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.9.2.1.2 The wcsncpy_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <wchar.h>

int main(void)
{
/* ... */
wchar_t src1[100] = L"hello";
wchar_t src2[7] = {L'g', L'o', L'o', L'd', L'b', L'y', L'e'};
wchar_t dst1[6], dst2[5], dst3[5];
int r1, r2, r3;
r1 = wcsncpy_s(dst1, 6, src1, 100);
r2 = wcsncpy_s(dst2, 5, src2, 7);
r3 = wcsncpy_s(dst3, 5, src2, 4);
return printf("K.3.9.2.1.2 The wcsncpy_s function %d %d %d  \n",r1,r2,r3 );
}
// error
Debug Assertion Failed!
File: f:dd:vctools\crtw32\h\tcsncat_s.ini
Line:72
Expression("Buffer is too small" && 0)
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 K.3.7.3.1 The strtok_s function // Example

// filename:c2011-K-3-7-3-1-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.7.3.1 The strtok_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>

int main(void)
{
/* ... */ 
static char str1[] = "?a???b,,,#c";
static char str2[] = "\t \t";
char *t, *ptr1, *ptr2;
rsize_t max1 = sizeof (str1);
rsize_t max2 = sizeof (str2);
t = strtok_s(str1, &max1, "?", &ptr1); // t points to the token "a"
printf("%s", t);
t = strtok_s(NULL, &max1, ",", &ptr1); // t points to the token "??b"
printf("%s", t);
t = strtok_s(str2, &max2, " \t", &ptr2); // t is a null pointer
printf("%s", t);
t = strtok_s(NULL, &max1, "#,", &ptr1); // t points to the token "c"
printf("%s", t);
t = strtok_s(NULL, &max1, "?", &ptr1); // t is a null pointer
return printf(" K.3.7.3.1 The strtok_s function%s \n",t );
}
// output may be
-----------------------------------------
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include "stdafx.h"
// filename:c2011-K-3-7-3-1-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.7.3.1 The strtok_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
 

int main(void)
{
 /* ... */
 static char str1[] = "?a???b,,,#c";
 static char str2[] = "\t \t";
 char *t, *ptr1, *ptr2;
 rsize_t max1 = sizeof (str1);
 rsize_t max2 = sizeof (str2);
 t = strtok_s(str1,/* &max1,*/ "?", &ptr1); // t points to the token "a"
 printf("%s", t);
 t = strtok_s(NULL, /*&max1,*/ ",", &ptr1); // t points to the token "??b"
 printf("%s", t);
 t = strtok_s(str2, /*&max2,*/ " \t", &ptr2); // t is a null pointer
 printf("%s", t);
 t = strtok_s(NULL, /*&max1,*/ "#,", &ptr1); // t points to the token "c"
 printf("%s", t);
 t = strtok_s(NULL, /*&max1,*/ "?", &ptr1); // t is a null pointer
 return printf(" K.3.7.3.1 The strtok_s function %s \n", t);
}
//
//エラー 1 error C2660: 'strtok_s' : 関数に 4 個の引数を指定できません。 c:\users\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 53 1 ConsoleApplication1

// output may be
//a??b(null)c K.3.7.3.1 The strtok_s function (null)
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 K.3.7.2.2 The strncat_s function // Example

// filename:c2011-K-3-7-2-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.7.2.2 The strncat_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>

int main(void)
{
/* ... */
char s1[100] = "good";
char s2[6] = "hello";
char s3[6] = "hello";
char s4[7] = "abc";
char s5[1000] = "bye";
int r1, r2, r3, r4;
r1 = strncat_s(s1, 100, s5, 1000);
r2 = strncat_s(s2, 6, "", 1);
r3 = strncat_s(s3, 6, "X", 2);
r4 = strncat_s(s4, 7, "defghijklmn", 3);
return printf("K.3.7.2.2 The strncat_s function %d %d %d %d\n",r1,r2,r3,r4 );
}
// error
Debug Assertion Failed!
File: f:dd:vctools\crtw32\h\tcsncat_s.ini
Line:72
Expression("Buffer is too small" && 0)
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

N1570 typo

section, paragraph, wrong -> correct, reason.
K.3.7.1.4 , 7, Example 1 -> Example, this section has only one example.
K.3.7.2.2 , 8, Example 1 -> Example, this section has only one example.
K.3.9.2.2.2 , 15, Example 1 -> Example, this section has only one example.

<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 K.3.7.1.4 The strncpy_s function

// filename:c2011-K-3-7-1-4-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.7.1.4 The strncpy_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>

int main(void)
{
/* ... */
char src1[100] = "hello";
char src2[7] = {'g', 'o', 'o', 'd', 'b', 'y', 'e'};
char dst1[6], dst2[5], dst3[5];
int r1, r2, r3;
r1 = strncpy_s(dst1, 6, src1, 100);
r2 = strncpy_s(dst2, 5, src2, 7);
r3 = strncpy_s(dst3, 5, src2, 4);
return printf("K.3.7.1.4 The strncpy_s function %d %d %d \n",r1,r2,r3 );
}
// error
Debug Assertion Failed!
File: f:dd:vctools\crtw32\h\tcsncat_s.ini
Line:72
Expression("Buffer is too small" && 0)
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 K.3.5.3.2 The fscanf_s function // Examples

// filename:c2011-K-3-5-3-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.5.3.2 The fscanf_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>

int main(void)
{
/* ... */
int n, i; float x; char name[50];
n = fscanf_s(stdin, "%d%f%s", &i, &x, name, (rsize_t) 50);
printf( "%d%f%s\n", i, x, name, (rsize_t) 50);

// Example 2

/* ... */
//int n; 
char s[5];
n = fscanf_s(stdin, "%s", s, sizeof s);
printf( "%s %d \n", s, sizeof s);
return printf("K.3.5.3.2 The fscanf_s function \n");
}
// output may be 
------------------------------------------
// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include "stdafx.h"
// filename:c2011-K-3-5-3-2-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 K.3.5.3.2 The fscanf_s function
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// Example 1
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
 

int main(void)
{
 /* ... */
 int n, i; float x; char name[50];
 n = fscanf_s(stdin, "%d%f%s", &i, &x, name, (rsize_t)50);
 printf("%d%f%s\n", i, x, name, (rsize_t)50);
 

 // Example 2
 

 /* ... */
 //int n;
 char s[5];
 n = fscanf_s(stdin, "%s", s, sizeof s);
 printf("%s %d \n", s, sizeof s);
 return printf("K.3.5.3.2 The fscanf_s function \n");
}
// input from console
// 123
// abc
// output may be
// 123 - 107374176.000000フフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフフ{
// abc 5
//  K.3.5.3.2 The fscanf_s function
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 G.5.1 Multiplicative operators // Examples

// filename:c2011-G-5-1-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 G.5.1 Multiplicative operators
// compile and output mecG.5.1 Multiplicative operatorshanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
#include <stdio.h>

// Example 1
#include <math.h>
#include <complex.h>
/* Multiplyz * w... */
double complex _Cmultd(double complex z, double complex w)
{
#pragma STDC FP_CONTRACT OFF
double a, b, c, d, ac, bd, ad, bc, x, y;
a = creal(z); b = cimag(z);
c = creal(w); d = cimag(w);
ac = a * c; bd = b * d;
ad = a * d; bc = b * c;
x = ac - bd; y = ad + bc;
if (isnan(x) && isnan(y)) {
/* Recover infinities that computed as NaN+iNaN ... */
int recalc = 0;
if (isinf(a) || isinf(b)) { // z is infinite
/* "Box" the infinity and change NaNs in the other factor to 0 */
a = copysign(isinf(a) ? 1.0 : 0.0, a);
b = copysign(isinf(b) ? 1.0 : 0.0, b);
if (isnan(c)) c = copysign(0.0, c);
if (isnan(d)) d = copysign(0.0, d);
recalc = 1;
}
if (isinf(c) || isinf(d)) { // w is infinite
/* "Box" the infinity and change NaNs in the other factor to 0 */
c = copysign(isinf(c) ? 1.0 : 0.0, c);
d = copysign(isinf(d) ? 1.0 : 0.0, d);
if (isnan(a)) a = copysign(0.0, a);
if (isnan(b)) b = copysign(0.0, b);
recalc = 1;
}
if (!recalc && (isinf(ac) || isinf(bd) ||
isinf(ad) || isinf(bc))) {
/* Recover infinities from overflow by changing NaNs to 0 ... */
if (isnan(a)) a = copysign(0.0, a);
if (isnan(b)) b = copysign(0.0, b);
if (isnan(c)) c = copysign(0.0, c);
if (isnan(d)) d = copysign(0.0, d);
recalc = 1;
}
if (recalc) {
x = INFINITY * ( a * c - b * d );
y = INFINITY * ( a * d + b * c );
}
}
return x + I * y;
}
// Example 2
/* Dividez / w ... */
double complex _Cdivd(double complex z, double complex w)
{
#pragma STDC FP_CONTRACT OFF
double a, b, c, d, logbw, denom, x, y;
int ilogbw = 0;
a = creal(z); b = cimag(z);
c = creal(w); d = cimag(w);
logbw = logb(fmax(fabs(c), fabs(d)));
if (isfinite(logbw)) {
ilogbw = (int)logbw;
c = scalbn(c, -ilogbw); d = scalbn(d, -ilogbw);
}
denom = c * c + d * d;
x = scalbn((a * c + b * d) / denom, -ilogbw);
y = scalbn((b * c - a * d) / denom, -ilogbw);
/* Recover infinities and zeros that computed as NaN+iNaN; */
/* the only cases are nonzero/zero, infinite/finite, and finite/infinite, ... */
if (isnan(x) && isnan(y)) {
if ((denom == 0.0) &&
(!isnan(a) || !isnan(b))) {
x = copysign(INFINITY, c) * a;
y = copysign(INFINITY, c) * b;
}
else if ((isinf(a) || isinf(b)) &&
isfinite(c) && isfinite(d)) {
a = copysign(isinf(a) ? 1.0 : 0.0, a);
b = copysign(isinf(b) ? 1.0 : 0.0, b);
x = INFINITY * ( a * c + b * d );
y = INFINITY * ( b * c - a * d );
}
else if ((logbw == INFINITY) &&
isfinite(a) && isfinite(b)) {
c = copysign(isinf(c) ? 1.0 : 0.0, c);
d = copysign(isinf(d) ? 1.0 : 0.0, d);
x = 0.0 * ( a * c + b * d );
y = 0.0 * ( b * c - a * d );
}
}
return x + I * y;
}
int main(void)
{
double complex z, w;
double complex d = _Cmultd( z, w);
double complex e =_Cdivd( z,  w);
return printf("G.5.1 Multiplicative operators %f %f\n", d, e);
}
// output may be 
//G.5.1 Multiplicative operators 0.000000 0.000000
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0

C2011 F.9.3 Relational operators // Example

// filename:c2011-F-9-3-ex.c
// original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
// C2011 F.9.3 Relational operators
// compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013
// compile errors and/or wornings:
// (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
// Target: x86_64-apple-darwin11.4.2 //Thread model: posix
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <stdarg.h>

// Example
void f(void){printf("f\n");}
void g(void){printf("g\n");}
int main(void)
{
float a,b;
// calls g and raises ‘‘invalid’’ if a and b are unordered
if (a < b)
f();
else
g();
// calls f and raises ‘‘invalid’’ if a and b are unordered
if (a >= b)
g();
else
f();

// calls f without raising ‘‘invalid’’ if a and b are unordered
if (isgreaterequal(a,b))
g();
else
f();
// calls g without raising ‘‘invalid’’ if a and b are unordered
if (isless(a,b))
f();
else
g();
if (!(a < b))
g();
else
f();

return printf("F.9.3 Relational operators\n");
}
// output may be 
// g
// g
// g
// g
// g
// F.9.3 Relational operators
<この稿は書きかけです。順次追記しています。>

twitter:@kaizen_nagoya
0