עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

i=rest, ואחרי השורה הזו, איכשהו i לא שווה rest... איך? C++

Featured Replies

פורסם

	do {
rest*=10;
cout<<rest<<endl;
i=rest;
cout<<i<<endl;
} while(rest!=rest);

זה הקוד (לפחות חלק ממנו)

rest מוגדר כdouble

i מוגדר כint

יש לי מחיר - משתנה double בשם price

עשיתי ככה:

int i = price;
double rest = price-i;

ככה שמה שנשאר לי בrest זה המחיר פחות החלק השלם שלו - המספר שאחרי הנקודה

עכשיו כמו ששמתם לב בקוד הזה אני מכפיל את זה ב10 כל הזמן ומשווה את זה לi, כדי שבסופו של דבר יהיה לי בi את המספר שאחרי הנקודה בprice

עשיתי רק מספר אחד אחרי הנקודה, אני יודע שהתנאי עצירה אדיוטי, אבל עשיתי את זה בגלל שזה נכנס ללולאה אינסופית - במקור זה אמור להיות i!=rest

בכל מקרה

ההדפסה מראה לי שrest = 8 אחרי ההכפלה ב10

אחרי זה מכניסים לi את הערך של rest ומדפיסים אותו - זה יוצא 7?!

מישהו יכול לעזור לי?

תודה

איל

נ.ב.

זה מה שקורה כש price = 60.8

ועכשיו כשהחלפתי את price לfloat כתוב לי שאחרי הנקודה זה 0.79999999...

זה אמור להיות 0.8!

=//

פורסם

יש כמה דברים שאתה צריך להבין לגבי floating point. הם לא מדוייקים.

אתה רוצה לעגל את התוצאה למעלה, למטה או לפי השבר, לפני שאתה ממיר למספר שלם.


i = floor(rest);
or
i = floor(rest + 0.5); // rounds rest

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.