Click here to Skip to main content
12,896,953 members (61,245 online)

C / C++ / MFC

 
QuestionRe: Empty String Pin
CPallini2-Jan-13 10:09
mvpCPallini2-Jan-13 10:09 
QuestionMFC Grid control 2.27 Pin
BorisAnimal30-Dec-12 4:46
memberBorisAnimal30-Dec-12 4:46 
AnswerRe: MFC Grid control 2.27 Pin
Richard MacCutchan30-Dec-12 5:34
mvpRichard MacCutchan30-Dec-12 5:34 
QuestionWhen should printf round a floating number up for display? Pin
Member 419459328-Dec-12 5:11
memberMember 419459328-Dec-12 5:11 
Specifically this statement:
printf("Chi square distribution for %s samples is %1.2f (%1.4f), and randomly\n",
   String_Value, chisq, chisq);


I re-implemented this program in MASM to speed it up and I get the following difference (note that I added the (%1.4f)) to display the pre-rounded number to see why I was getting differences:

------------------------------------------------------------------ 17
Chi square distribution for 2048 samples is 6.13 (6.1250), and randomly
-------------------------------------------------------------- 17
Chi square distribution for 2048 samples is 6.12 (6.1250), and randomly


The question is, which one is correct according to the C spec? Should 6.1250 round up to 6.13 or should it take 6.1250+ to round up?

There are other questions such as the following:
printf("of this %s %s file by %d (%2.2f) percent.\n\n", String_Value, samp,
   (short) (((100 * ((binary ? 1 : 8) - ent) / (binary ? 1.0 : 8.0)) + 0.5)),
            ((100 * ((binary ? 1 : 8) - ent) / (binary ? 1.0 : 8.0))));


The cast to a (short) seems to truncate a 5.7 to 5, I had to add the (.... + 0.5) just before the cast in order to get the rounded number. Again, I added the (%2.2f) to display the rounding differences.

Which is correct according to the C spec, rounding or truncation?

The program being re implemented is John Walker's ENT.

Dave.
AnswerRe: When should printf round a floating number up for display? Pin
Richard MacCutchan28-Dec-12 5:53
mvpRichard MacCutchan28-Dec-12 5:53 
GeneralRe: When should printf round a floating number up for display? Pin
Member 419459328-Dec-12 6:55
memberMember 419459328-Dec-12 6:55 
GeneralRe: When should printf round a floating number up for display? Pin
Richard MacCutchan28-Dec-12 7:01
mvpRichard MacCutchan28-Dec-12 7:01 
GeneralRe: When should printf round a floating number up for display? Pin
Member 419459328-Dec-12 7:08
memberMember 419459328-Dec-12 7:08 
GeneralRe: When should printf round a floating number up for display? Pin
Richard MacCutchan28-Dec-12 7:18
mvpRichard MacCutchan28-Dec-12 7:18 
GeneralRe: When should printf round a floating number up for display? Pin
Member 419459328-Dec-12 7:20
memberMember 419459328-Dec-12 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 29 Apr 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid