Solved

# help me!

Posted on 1999-11-07
Medium Priority
306 Views
I can't find the function 'trunc()' 's lib in borland c++ builder ,please tell me the lib name.

thank you!
0
Question by:afeng

LVL 4

Accepted Solution

nils pipenbrinck earned 150 total points
ID: 2189425
I'm not sure.. but trunc is used to round from float to int, isn't it?

standard clib gives you two different rounding functions. they are called

float floor (float) // round up
float ceil  (float) // round down

(defined in math.h or float.h)

If you only want the whole part of a
float number you can cast it to an
integer. I think that's the best match
to the trunc function.

Hope you don't speak about truncation of files btw. If so I could take a look at stdio.h There is a function to do this, but I rarely need it.

0

LVL 22

Expert Comment

ID: 2189440
nils_pipenbrinck is right, there is no trunc() in standard C/C++.  I beleive there is one in Pascal, perhaps that is what you were thinking of.  The floor() function is the equivilant one, or just converting to int.
0

LVL 7

Expert Comment

ID: 2190525
Keep in mind that floor finds the largest integer not greater than x. Thus floor(-4.5) == -5
0

LVL 85

Expert Comment

ID: 2190552
casting to an integral type will truncate, but the behavior is undefined if the value anoot be represented by the integral type.

modf (double value, double *iptr) will store the truncated value in *iptr
0

LVL 4

Expert Comment

ID: 2191550
ozo. the behaviour of casting is not undefined.. the ansi c standard describes which rounding mode to use.

I'm absolutely sure about that.. the intel c++ compiler is the only compiler where you can disable this behaviour for a little extra speed.

nils
0

LVL 85

Expert Comment

ID: 2191675
ANSI specifies that rounding mode used when casting to an integral type is truncation toward zero.
ANSI also syas that if the value of the integral part cannot br represented by the integral type, the behavior is undefined.
(thus floating values greater than ULONG_MAX are not portable)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll