Click here to Skip to main content
15,915,603 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Rounding Pin
Christian Graus17-May-05 16:37
protectorChristian Graus17-May-05 16:37 
GeneralRe: Rounding Pin
Shog917-May-05 17:53
sitebuilderShog917-May-05 17:53 
GeneralRe: Rounding Pin
Bob Stanneveld17-May-05 22:19
Bob Stanneveld17-May-05 22:19 
GeneralRe: Rounding Pin
Shog917-May-05 14:38
sitebuilderShog917-May-05 14:38 
GeneralRe: Rounding Pin
knapak17-May-05 15:00
knapak17-May-05 15:00 
GeneralRe: Rounding Pin
PJ Arends17-May-05 16:51
professionalPJ Arends17-May-05 16:51 
GeneralRe: Rounding Pin
G_S17-May-05 17:10
G_S17-May-05 17:10 
GeneralRe: Rounding Pin
cmk17-May-05 19:01
cmk17-May-05 19:01 
The method PJ showed, and you expanded for negative numbers, is a standard way.

Shog hinted at another way doing it using _controlfp (or _control87).
The rounding control flags specify how the assembly instruction frndint will round.

From Intel info on frndint:
The Control Word 16-bit register is used ...
The RC field (bits 11 and 10) or Rounding Control determines how the
FPU will round results in one of four ways:
00 = Round to nearest, or to even if equidistant (this is the initialized state)
01 = Round down (toward -infinity)
10 = Round up (toward +infinity)
11 = Truncate (toward 0)


So, by default, it rounds the way you want.
The problem is i don't know of any C function that calls this, hence the workaround of adding 0.5 and then truncating by cast to int.

A more general (non-portable) solution would be to write your own function that calls frndint.
e.g.

__forceinline  double  FkRound( double N )
{
    __asm {
        fld    N
        frndint
    }
}

Before calling this you could call _controlfp() to set the way you want rounding to work, or just to make sure it hasn't been changed by some other code.

This works for both positive and negative numbers.


...cmk

Save the whales - collect the whole set
Generaltary icon popup text Pin
eero_p17-May-05 12:00
eero_p17-May-05 12:00 
GeneralRe: tary icon popup text Pin
Peter Weyzen17-May-05 12:55
Peter Weyzen17-May-05 12:55 
GeneralRe: tary icon popup text Pin
eero_p17-May-05 20:54
eero_p17-May-05 20:54 
GeneralRe: tary icon popup text Pin
Peter Weyzen18-May-05 6:15
Peter Weyzen18-May-05 6:15 
GeneralProject Pin
Anonymous17-May-05 11:11
Anonymous17-May-05 11:11 
GeneralRe: Project Pin
Christian Graus17-May-05 13:30
protectorChristian Graus17-May-05 13:30 
GeneralRe: Project Pin
Anonymous17-May-05 15:19
Anonymous17-May-05 15:19 
GeneralRe: Project Pin
Christian Graus17-May-05 15:23
protectorChristian Graus17-May-05 15:23 
GeneralRe: Project Pin
Anonymous17-May-05 15:30
Anonymous17-May-05 15:30 
GeneralRe: Project Pin
Anonymous17-May-05 17:17
Anonymous17-May-05 17:17 
GeneralRe: Project Pin
FlyingTinman17-May-05 22:40
FlyingTinman17-May-05 22:40 
GeneralRe: Project Pin
David Crow18-May-05 2:32
David Crow18-May-05 2:32 
GeneralRe: Project Pin
Bob Stanneveld18-May-05 2:42
Bob Stanneveld18-May-05 2:42 
GeneralRe: Project Pin
Anonymous18-May-05 9:36
Anonymous18-May-05 9:36 
GeneralVisual C++ .NET 2003 Suppress method Pin
Gagnon Claude17-May-05 8:59
Gagnon Claude17-May-05 8:59 
GeneralRe: Visual C++ .NET 2003 Suppress method Pin
Chris Losinger17-May-05 9:49
professionalChris Losinger17-May-05 9:49 
GeneralRe: Visual C++ .NET 2003 Suppress method Pin
Christian Graus17-May-05 14:31
protectorChristian Graus17-May-05 14:31 

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.