69,371
社区成员
发帖
与我相关
我的任务
分享
...
#define DBL_DIG 15 /* # of decimal digits of precision */
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP 308 /* max decimal exponent */
#define DBL_MAX_EXP 1024 /* max binary exponent */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
#define DBL_MIN_EXP (-1021) /* min binary exponent */
#define _DBL_RADIX 2 /* exponent radix */
#define _DBL_ROUNDS 1 /* addition rounding: near */
#define FLT_DIG 6 /* # of decimal digits of precision */
#define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD 0
#define FLT_MANT_DIG 24 /* # of bits in mantissa */
#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MAX_10_EXP 38 /* max decimal exponent */
#define FLT_MAX_EXP 128 /* max binary exponent */
#define FLT_MIN 1.175494351e-38F /* min positive value */
#define FLT_MIN_10_EXP (-37) /* min decimal exponent */
#define FLT_MIN_EXP (-125) /* min binary exponent */
#define FLT_NORMALIZE 0
#define FLT_RADIX 2 /* exponent radix */
#define FLT_ROUNDS 1 /* addition rounding: near */
...
#include <iostream>
using namespace std;
int main()
{
float a=0.0;
for(int i=0; i<100; i++)
a += 0.1f;
cout <<a;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a=0.0;
for(int i=0; i<100; i++)
a += 0.1f;
cout << setprecision(10) << a << endl;
return 0;
}
你再把上面的0.1f改成1.5f试试
//Round(1.234,2) = 1.23
//Round(1.234,0) = 1.0
//Round(123.4,-1) = 120.0
double Round(double dVal, short iPlaces) {
double dRetval;
double dMod = 0.0000001;
if (dVal<0.0) dMod=-0.0000001;
dRetval=dVal;
dRetval+=(5.0/pow(10.0,iPlaces+1.0));
dRetval*=pow(10.0,iPlaces);
dRetval=floor(dRetval+dMod);
dRetval/=pow(10.0,iPlaces);
return(dRetval);
}
double round(double dVal, short iPlaces) //iPlaces>=0
{
unsigned char s[20];
double dRetval;
sprintf(s,"%.*lf",iPlaces,dVal);
sscanf(s,"%lf",&dRetval);
return (dRetval);
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd,&rc);
SetMapMode(hdc, MM_LOENGLISH);
SetViewportOrgEx(hdc,rc.right/2,rc.bottom/2,NULL);
//draw y=100sin(x) sine line
MoveToEx(hdc,(int)(x),0,NULL);
while(x<360.0f)
{
y=(float)sin(x/57.3)*100;
LineTo(hdc,(int)(x),(int)y);
MoveToEx(hdc,(int)(x),(int)y,NULL);
x+=1.5f;
}
for(x=-360.0f;x<=360.0f;x+=90.0f)
{
TCHAR buf[40] = {0};
y=(float)sin(x/57.3)*100;
if(fabs(y-0.0f)<0.00001f)
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x+4.0f,y+8.0f,buf,lstrlen(buf));
}
else if( fabs(y-100.0f)<0.00001f )
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x+4.0f,y+8.0f,buf,lstrlen(buf));
}
else if(fabs(y+100.0f)<0.00001f)
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x+4.0f,y-8.0f,buf,lstrlen(buf));
}
}
EndPaint(hwnd, &ps);
return 0;
}
return 0;
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <math.h>
TCHAR szAppName[] = TEXT("Test");
LRESULT CALLBACK TestWinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
HWND hwnd;
BOOL bRet;
MSG msg;
hInstance = hInstance;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = TestWinProc;
wndcls.lpszClassName = szAppName;
wndcls.lpszMenuName = NULL;//无菜单
wndcls.style = CS_VREDRAW | CS_HREDRAW;
if (!RegisterClass(&wndcls))
{
MessageBox(NULL, TEXT("此程序需要在WINDOWS NT操作系统下运行!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Test"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (-1 == bRet)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK TestWinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static int cxChar;
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
RECT rc;
float x=-360.0f; float y=0.0f;// for draw sine line
switch(uMsg)
{
case WM_CREATE:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
return 0;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd,&rc);
SetMapMode(hdc, MM_LOENGLISH);
SetViewportOrgEx(hdc,rc.right/2,rc.bottom/2,NULL);
//draw y=100sin(x) sine line
MoveToEx(hdc,(int)(x),0,NULL);
while(x<360.0f)
{
y=(float)sin(x/57.3)*100;
LineTo(hdc,(int)(x),(int)y);
x+=1.5f;
}
for(x=-360.0f;x<=360.0f;x+=90.0f)
{
TCHAR buf[40] = {0};
y=(float)sin(x/57.3)*100;
if(fabs(y-0.0f)<0.05f)
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x,y,buf,lstrlen(buf));
}
else if(fabs(y-100.0f)<0.05f)
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x,y,buf,lstrlen(buf));
}
else if(fabs(y+100.0f)<0.05f)
{
_stprintf(buf,_T("x=%d度;y=%f"),(int)x,y);
TextOut(hdc,x,y,buf,lstrlen(buf));
}
}
EndPaint(hwnd, &ps);
return 0;
}
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}