11 Replies - 3215 Views - Last Post: 19 November 2010 - 07:17 PM Rate Topic: -----

#1 happilymambo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-November 10

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

Posted 19 November 2010 - 12:44 PM

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?

Is This A Good Question/Topic? 0
  • +

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

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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?
Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Was This Post Helpful? 1
  • +
  • -

#4 Guest_Guest*


Reputation:

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.
Was This Post Helpful? 0

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

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

Posted 19 November 2010 - 02:55 PM

From man sqrt:

Quote

long double sqrtl(long double x);


Note the trailing lower-case L
Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

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

Posted 19 November 2010 - 03:01 PM

Interesting, since cplusplus from janotte's link shows now trailing l.
Time to test...
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

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

Posted 19 November 2010 - 03:08 PM

From the page to which janotte linked:

Quote

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


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.

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

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

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.
Was This Post Helpful? 0

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

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:
#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


Was This Post Helpful? 1
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 19 November 2010 - 06:22 PM

View PostGuest, 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 has had done anything inappropriate. The only 'bad' person here is me.

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

Was This Post Helpful? 1
  • +
  • -

#11 happilymambo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-November 10

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.
Was This Post Helpful? 0
  • +
  • -

#12 happilymambo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-November 10

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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1