Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
chglcu
May 17, 2007

I'm so bored with the USA.

Thug Bonnet posted:

That is super clever, but it's not my struct :(

In that case, you're stuck setting the members of the struct in the body of the class constructor, barring some language feature I'm unaware of.

Adbot
ADBOT LOVES YOU

floWenoL
Oct 23, 2002

prolecat posted:

In that case, you're stuck setting the members of the struct in the body of the class constructor, barring some language feature I'm unaware of.

Since structs have a default copy constructor, you can define a function that returns said struct (something like MyStruct MakeDefaultMyStruct()) and initialize the member variable with that.

Groceries Stealer
Jul 30, 2003

StickGuy posted:

Are there any software packages out there that can analyze C/C++ floating point calculations to identify sensitive ones or suggest different orders of operations? Ideally something that could check based on the actual values used in the calculations.

The standard for linear algebra is LAPACK, Matlab is actually build on top of it. GSL (GNU Scientific Library: http://www.gnu.org/software/gsl/) is a good wrapper, since the original Fortran -> C interface can be quite daunting at first, especially with function names like DGESV.

*EDIT* Quoted the wrong post, this is an answer for Chin Strap.

Groceries Stealer fucked around with this message at 19:17 on Jul 31, 2008

Lexical Unit
Sep 16, 2003

prolecat posted:

In that case, you're stuck setting the members of the struct in the body of the class constructor, barring some language feature I'm unaware of.
Well he could do
code:
TestObject::TestObject(int _struct_member):ts((test_struct){_struct_member}){ }
if he wanted, but he should really do what floWenoL said.

StickGuy
Dec 9, 2000

We are on an expedicion. Find the moon is our mission.

FrankFerl posted:

The standard for linear algebra is LAPACK, Matlab is actually build on top of it. GSL (GNU Scientific Library: http://www.gnu.org/software/gsl/) is a good wrapper, since the original Fortran -> C interface can be quite daunting at first, especially with function names like DGESV.
It's more that I already have a C++ program that performs lots of complicated floating point calculations and I want an automated way of finding and improving places where accuracy may be being lost.

Big Mark
Feb 4, 2003
eighties computer game avatar

Chin Strap posted:

Figure this is a good place to ask:

I'm used to doing most of my programming in things like R and Matlab, but I want to go to C++ in anticipation of project sizes that aren't going to work well in scripted languages. What are some good libraries out there for linear algebra and statistics?
I've always liked NAG, which is an expensive bazillion meg library file that you link against. Its Fortran 55BC heritage has bequeathed it with a hairy C/C++ interface but it works correctly and is fast.

chglcu
May 17, 2007

I'm so bored with the USA.

floWenoL posted:

Since structs have a default copy constructor, you can define a function that returns said struct (something like MyStruct MakeDefaultMyStruct()) and initialize the member variable with that.

Lexical Unit posted:

Well he could do
code:
TestObject::TestObject(int _struct_member):ts((test_struct){_struct_member}){ }
if he wanted, but he should really do what floWenoL said.

Yes, but in both cases, you're creating an extra temporary copy of the struct, in addition to not using initialization lists to initialize its members, correct? I'm guessing that copy would probably be optimized out, but still.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

prolecat posted:

Yes, but in both cases, you're creating an extra temporary copy of the struct, in addition to not using initialization lists to initialize its members, correct? I'm guessing that copy would probably be optimized out, but still.

But still what? Always write code that's easier to read first, and if it turns out to be slow for whatever reason, then optimize it.

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

But still what? Always write code that's easier to read first, and if it turns out to be slow for whatever reason, then optimize it.

Well then putting aside the initialization list it seems like just doing assignment in the constructor would be more readable?

chglcu
May 17, 2007

I'm so bored with the USA.

Avenging Dentist posted:

But still what? Always write code that's easier to read first, and if it turns out to be slow for whatever reason, then optimize it.

I have to disagree when the optimization in question is something that's already very easy to do and just as readable (in my opinion). No sense being slow when there's really no reason.

That Turkey Story
Mar 30, 2003

Thug Bonnet posted:

Well then putting aside the initialization list it seems like just doing assignment in the constructor would be more readable?

As long as you have all trivial datamembers or if you aren't that concerned with efficiency, use whichever one you find easier to read. It won't matter.

prolecat posted:

I have to disagree when the optimization in question is something that's already very easy to do and just as readable (in my opinion). No sense being slow when there's really no reason.

If you are so concerned about optimization, all floWenoL's version needs out of a compiler is NRVO support (which most if not all modern compilers support), and in the case of a struct with non-trivial assignment, floWenoL's version will likely be more optimized whether NRVO is supported or not.

Edit: In the case of all trivial datamembers, I agree, the version with assignments done in the constructor is the simplest approach and likely just as efficient.

That Turkey Story fucked around with this message at 18:08 on Jul 31, 2008

Groceries Stealer
Jul 30, 2003

StickGuy posted:

It's more that I already have a C++ program that performs lots of complicated floating point calculations and I want an automated way of finding and improving places where accuracy may be being lost.

Sorry, I actually quoted the wrong post. I was answering this one:

Chin Strap posted:

Figure this is a good place to ask:

I'm used to doing most of my programming in things like R and Matlab, but I want to go to C++ in anticipation of project sizes that aren't going to work well in scripted languages. What are some good libraries out there for linear algebra and statistics?

Entheogen
Aug 30, 2004

by Fragmaster
is there a way to determine endianess of a floating point number at runtime in either C++ or Java? Is this something that cannot be determined from data alone? I am reading a binary file and would like to know if I have to reverse the bits or not.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Entheogen posted:

is there a way to determine endianess of a floating point number at runtime in either C++ or Java? Is this something that cannot be determined from data alone? I am reading a binary file and would like to know if I have to reverse the bits or not.

Why wouldn't you do that at compile time?

Entheogen
Aug 30, 2004

by Fragmaster

Avenging Dentist posted:

Why wouldn't you do that at compile time?

What do you mean? The idea is that I will have binary files as input and some of them might have IEEE floating point numbers in one endianness and another in different one. Instead of forcing user to choose which one they would like the file to be read as, I was wondering if there is an automatic way of doing this, but my suspicion is there is not because otherwise there would have to be some sort of meta-data describing the contents of file, and there is no such data, its just floating point numbas.

litghost
May 26, 2004
Builder

Avenging Dentist posted:

Why wouldn't you do that at compile time?

Because you might be reading from a source with the endian-ness swapped.

Entheogen posted:

is there a way to determine endianess of a floating point number at runtime in either C++ or Java? Is this something that cannot be determined from data alone? I am reading a binary file and would like to know if I have to reverse the bits or not.

Software I worked on knows if endian-ness is swapped by checking known fields. Example, the month as a 32-bit integer. It should never be 0, and will not be greater than 12. If you read it as 213134142 or something, swapped endian-ness. This will only work if you can find a piece of data like this. We just fixed a bug where the date was set to 0/0/0 and it broke the endian swap code, so this is fragile. However, it is better than nothing.

IEEE floating point also has values that should never exist (I think). You could search for these and that could tell you to swap.

:edit: If you can guarantee no NaN or Inf values in the data, looking for NaN or Inf values could also be a hint.

litghost fucked around with this message at 03:42 on Aug 1, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

litghost posted:

Because you might be reading from a source with the endian-ness swapped.

That's stupid.

Edit for content: Seriously, what kind of rear end in a top hat would make a binary file specification without defining the endianness?

Avenging Dentist fucked around with this message at 04:27 on Aug 1, 2008

Scaevolus
Apr 16, 2007

litghost posted:

Software I worked on knows if endian-ness is swapped by checking known fields. Example, the month as a 32-bit integer. It should never be 0, and will not be greater than 12. If you read it as 213134142 or something, swapped endian-ness. This will only work if you can find a piece of data like this. We just fixed a bug where the date was set to 0/0/0 and it broke the endian swap code, so this is fragile. However, it is better than nothing.

Why not just include a flag in the binary data that defines endianness?

edit: maybe I should refresh before posting

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Or just serialize to text if you have the choice.

litghost
May 26, 2004
Builder

Avenging Dentist posted:

That's stupid.

Edit for content: Seriously, what kind of rear end in a top hat would make a binary file specification without defining the endianness?

The reason most software is retarded, backwards compatibility. Hey I didn't write the thing, don't blame me.

:edit: Did I mention it was written in FORTRAN? Lots of GOTOs.

litghost fucked around with this message at 04:37 on Aug 1, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Avenging Dentist posted:

That's stupid.

Edit for content: Seriously, what kind of rear end in a top hat would make a binary file specification without defining the endianness?

I am reading in files made from Fortran where it is reverse. But I would also like my software to work with files generated with normal endianness. Ideally I don't want to rely on any sort of meta-data in the file itself to be able to successfully read it in and visualize it. I think I will try NaN and Infinite values perhaps or try to look for those bits that can never be true if its correct endianess. In worst case scenario I will just let the end-user decide what endianess they want before they read in the file.

litghost
May 26, 2004
Builder

Entheogen posted:

I am reading in files made from Fortran where it is reverse. But I would also like my software to work with files generated with normal endianness. Ideally I don't want to rely on any sort of meta-data in the file itself to be able to successfully read it in and visualize it. I think I will try NaN and Infinite values perhaps or try to look for those bits that can never be true if its correct endianess. In worst case scenario I will just let the end-user decide what endianess they want before they read in the file.

NaN and Inf are valid values if the software allows or expects it.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Entheogen posted:

I am reading in files made from Fortran where it is reverse. But I would also like my software to work with files generated with normal endianness. Ideally I don't want to rely on any sort of meta-data in the file itself to be able to successfully read it in and visualize it. I think I will try NaN and Infinite values perhaps or try to look for those bits that can never be true if its correct endianess. In worst case scenario I will just let the end-user decide what endianess they want before they read in the file.

What's wrong with a byte-order flag? I mean, that's how any sane person resolves this issue (see also: UTF-16). If it's a really big deal, just make the reader default to Fortran-endianness and have an optional byte-order flag.

Zombywuf
Mar 29, 2008

Entheogen posted:

... normal endianness.

No such thing.

quote:

I think I will try NaN and Infinite values perhaps or try to look for those bits that can never be true if its correct endianess. In worst case scenario I will just let the end-user decide what endianess they want before they read in the file.

So you want to use implicit metadata and not explicit metadata.

Enthogen posted:

Ideally I don't want to rely on any sort of meta-data in the file itself to be able to successfully read it in and visualize it.
In which case don't change the format of the file. Make your new programs spit it out in the same way the old programs.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
I've decided that The Apache C++ Standard Library Reference Guide is a great web reference and easier to use than SGI's. It covers the whole standard library (while SGI's covers only containers and iterators) and they make it more obvious when something is an extension. Descriptions are formatted nicely, too.

SGI's still has a place for its definitions of concepts, though.

newsomnuke
Feb 25, 2007

In C++, is there a standard way (in code) to stop compilation and display a custom message?

I know in MSVC you can use #pragma error, but I can't rely on this for other compilers.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
What phase of compilation? During preprocessing you can use #error.

newsomnuke
Feb 25, 2007

Mustach posted:

What phase of compilation? During preprocessing you can use #error.
Wonderful, thanks.

more falafel please
Feb 26, 2005

forums poster

ultra-inquisitor posted:

Wonderful, thanks.

Probably not what you need, but in case anyone else is wondering, you can also use Boost.StaticAssert to do this at compile time (if preprocessor time is too soon/not expressive enough).

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
That reminds me, why is it Boost convention to talk about the libraries using C# style? E.g. "Boost.StaticAssert" instead of "boost::static_assert".

Vanadium
Jan 8, 2005

Perhaps to make it clear you are talking about the project and not the namespace/class/function [template].

vvvv <:mad:>

Vanadium fucked around with this message at 23:32 on Aug 2, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Also stuff like Boost.SmartPointer isn't a namespace. They're all in the boost namespace.

more falafel please
Feb 26, 2005

forums poster

Mustach posted:

That reminds me, why is it Boost convention to talk about the libraries using C# style? E.g. "Boost.StaticAssert" instead of "boost::static_assert".

I don't know the reasoning behind it, but the Boost.Camel style is how you refer to the libraries. The actual identifiers are referred to in their C++ names, like boost::shared_ptr.

DickParasite
Dec 2, 2004


Slippery Tilde
Hey all,

This has probably been covered, but I'm looking to generate random numbers in a Gaussian distribution.

Through Google I've discovered the boost libraries, and I know they have a method for doing it. Unfortunately I'm not a professional programmer, so it's a little obtuse if I can't see an example.

Basically my input would be a mean, a sigma, and potentially an integer value for the number of randomly generator numbers I want outputted.

Can anyone post an example?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
This shows you how to do pretty much everything with Boost.Random: http://www.boost.org/doc/libs/1_35_0/libs/random/random_demo.cpp

ehnus
Apr 16, 2003

Now you're thinking with portals!
I've whipped a Gaussian random variate generator in C++ that generates numbers via a rejection technique and thrown in an implementation of the Mersenne Twister random number generator for fun. It has no dependencies aside from the standard math library

http://av8r.ca/~max/gaussian.cpp

The only thing to be aware of is that you need to call twister_initialize() before calling gaussian() otherwise you won't get any valid numbers.

DickParasite
Dec 2, 2004


Slippery Tilde
Thanks gents. I appreciate the help.

FearIt
Mar 11, 2007
Quick question, I'm trying to help a friend create a fuction that accepts a char[] as an input and outputs a char[]. How would I be able to do this?

char[] myFunct(char[] myArray)

normally I would use strings, but my friend wants to do it with only char[] so that it works with the rest of his code.

FearIt
Mar 11, 2007
actually, I am now doing that with pointers:

char* time_incr(char* time);

but how do I actually access individual chars in a char*, it just seems to spit out the offset until the null terminator each time..

Adbot
ADBOT LOVES YOU

KaeseEs
Feb 23, 2007

by Fragmaster

FearIt posted:

actually, I am now doing that with pointers:

char* time_incr(char* time);

but how do I actually access individual chars in a char*, it just seems to spit out the offset until the null terminator each time..

You can treat a pointer as an array, ex.
code:
char *mydata;
mydata = calloc(array_size, sizeof(*mydata));

mydata[0] = 'C';
mydata[1] = 'o';
mydata[2] = 'C';
mydata[3] = '\0';
...
free(mydata);

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply