I'm writing a C program using Borland's BCC32 compiler, and the program accrues roundoff error, so I'm using long doubles to preserve as many significant digits as possible. I was able to write my own macros for LDABS() and LDMOD() to replace fabs() and fmod(), but now I would like to do the same thing to replace sqrt() with an LDSQRT() macro. I'm sure this sort of thing has already been done a million times before, but I'm unaware of how to access any of the already written code. Any suggestions?

# Long double version of C's sqrt() function?

Page 1 of 1## 11 Replies - 4599 Views - Last Post: 19 November 2010 - 07:17 PM

##
**Replies To:** Long double version of C's sqrt() function?

### #2

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 01:42 PM

Why do you need to write a new function?

Why not pass just sqrt() a long double?

Read here the documentation

http://www.cplusplus...ary/cmath/sqrt/

How am I misunderstanding your question?

Why not pass just sqrt() a long double?

Read here the documentation

http://www.cplusplus...ary/cmath/sqrt/

How am I misunderstanding your question?

### #3

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 02:51 PM

**@ failbot**

If you are going to give me a neg rep then you need to give an explanation.

Why did I say that was not helpful?

Now you could have a perfectly valid reason for that down vote. Please tell us what it is.

Ahhh - I get it.

I was wrong. Well done.

Still would have been good for you to say why.

This post has been edited by **janotte**: 19 November 2010 - 02:59 PM

### #4 Guest_Guest*

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 02:52 PM

If the long double version of sqrt exists in C, then I need to know how to access it, rather than the double version of it. But, I'm going by the documentation you sent me, that only the double version exists in C. I need the extra 4 or 5 significant digits that get thrown away by using the double version rather than the long double version.

### #5

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 02:55 PM

### #6

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 03:01 PM

### #7

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 03:08 PM

From the page to which janotte linked:

EDIT:~~Note also, where you're using a somewhat non-standard (and old?) compiler in bcc32, it may in fact not be present. That would be a question for the compiler documentation I guess.~~

EDIT 2: Based on this 6-year-old thread elsewhere, it looks to work with bcc32 version 5.6.4.

EDIT 3: Embarcadero's website doc, but not knowing your version, don't know how valid it is.

Quote

Portability

In C, only the double version of this function exists with this name.

In C, only the double version of this function exists with this name.

EDIT:

EDIT 2: Based on this 6-year-old thread elsewhere, it looks to work with bcc32 version 5.6.4.

EDIT 3: Embarcadero's website doc, but not knowing your version, don't know how valid it is.

This post has been edited by **JackOfAllTrades**: 19 November 2010 - 03:14 PM

### #8 Guest_Guest*

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 03:09 PM

I think you must have been replying to someone else. I'm not sure what a "neg rep" is, or what "@ Failbot" means, but it sounds like someone gave some negative feedback - it wasn't me.

### #9

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 03:12 PM

Well then it looks like C++ is needed here, and sending a long double to sqrt() will not be sufficient:

output:

#include <iostream> #include <math.h> using namespace std; int main() { cout.precision(25); double sq = 3.0; long double sql = 3.0; cout <<sqrt(sql) << " \t"<< sqrt(sq) << " \t"<< sqrtl(sql)<< " \t"<< sqrtl(sq)<< endl; cin.get(); return 0; }

output:

1.732050807568877193176604 1.732050807568877193176604 1.732050807568877293573725 1.732050807568877293573725

### #10

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 06:22 PM

Guest, on 20 November 2010 - 07:09 AM, said:

I think you must have been replying to someone else. I'm not sure what a "neg rep" is, or what "@ Failbot" means, but it sounds like someone gave some negative feedback - it wasn't me.

Correct. The '@ failbot' bit meant I was talking to a DIC member called 'failbot'

I was asking why they voted my answer down as unhelpful.

Later you pointed out that I was completely and utterly wrong. The down vote from 'failbot' was completely appropriate just as you giving me a down vote of your own (if you had voting rights - why not sign up as a member and get such rights?, it's free and makes participating on DIC a better experience) would also have been completely appropriate. I gave you useless, unhelpful advice. In DIC that should be noted as such with a down vote That's how things work here.

The rest of that was suggesting that it would have been more useful for the member 'failbot' to have explained why I was wrong. Just like you did and

**JackOfAllTrades**said again later in the thread. That would have meant everyone reading this could have more quickly seen how I was utterly wrong and could have pointed and laughed just as they should and everyone could have more quickly seen my post as the useless twaddle that it was.

The other thing that 'failbot' missed out on by not giving their reason was allowing me, and possibly others, to give her/him positive votes for pointing out my blunder.

Sorry if this is hijacking your thread but I did want to respond to your question to assure you no-one thought you

This post has been edited by **janotte**: 19 November 2010 - 06:26 PM

### #11

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 06:53 PM

Sqrtl solves the problem. Thanks very much - I would never have guessed that they would append the "l" qualifier to the right side, when other C funcions like fmod or fabs append it to the left side. But then, there's modf, which does something completely different from fmod -- is the naming convention really that haphazard in C, or is there some unpercieved method to the madness?

Just out of curiosity and for future reference, if the sqrtl function had not been part of the C language, is there any way to view the actual source code that is used for things like sqrt? I was hoping I would be able to find it by looking in math.h, but not so. If I have to write macros for things like this in the future, it would be helpful to know where to look, or if that source code is even available for us mere mortals to view. Thanks again.

Just out of curiosity and for future reference, if the sqrtl function had not been part of the C language, is there any way to view the actual source code that is used for things like sqrt? I was hoping I would be able to find it by looking in math.h, but not so. If I have to write macros for things like this in the future, it would be helpful to know where to look, or if that source code is even available for us mere mortals to view. Thanks again.

### #12

## Re: Long double version of C's sqrt() function?

Posted 19 November 2010 - 07:17 PM

Wow, janotte, lighten up on yourself - no one expects absolute perfection from answers that are offered for free. Your willingness to take the time to help others speaks well of you. Anyone expecting absolutely flawless answers needs to be willing to pay big bucks for them, and should not be looking for them on a "for free" website.

Page 1 of 1