Re: market models code

Posted by Dominick Samperi on
URL: http://quantlib.414.s1.nabble.com/market-models-code-tp11949p11960.html

I discovered this odd property of valarray just weeks ago.

In his book Bjarne Stroustrup says the C++ standard on
valarray gives compiler writers much latitude so that they can use
tricks behind the scenes to speed up code.

Thus there may be a reason for this madness.

This is too bad, because valarray gives a nice way to do vector
processing and selecting (using valarray<bool>). Unfortunately,
you need to know how many items will be selected before the
code is run that does the selection! This greatly reduces the
utility and elegance of the approach.

Dominick

Mark joshi wrote:

> Ok you've convinced me that valarray is bad. I hate classes where =
> doesn't do what you expect!
>
> vector<bool> is bad too.
>
> Maybe we should be using a boost class for arrays of bools.
>
> best
>
> mark
>
>
> 2009/11/13 Luigi Ballabio <[hidden email]>:
>  
>> On Tue, 2009-11-10 at 14:07 +1100, Mark joshi wrote:
>>    
>>> as you'll have noticed I've been doing some fiddling with the market
>>> models code.
>>>      
>> Mark,
>>        just for future reference: when using gcc, the test suite was failing
>> hard (access violations and such.)  It turns out that according to the C
>> ++ standard, the valarray assignment operator is only required to work
>> when the two sides of the assignment have the same size (yes, one never
>> ends learning C++.)   More details in the standard and at
>> <http://groups.google.com/group/comp.lang.c
>> ++/browse_thread/thread/b9ac323fd7f5578b/dba133d7b15e91e0?hl=en&ie=UTF-8>.
>>
>> Therefore, when one writes
>>
>> valarray<Foo> v1;
>> v1 = some_function();
>>
>> where some_function returns a (presumably not empty) valarray, the
>> behavior is undefined (the above effectively occurs, for instance, when
>> a valarray data member is initialized in the constructor body.)  Visual
>> C++ tries to help the average programmer and does what one would expect
>> (i.e., resize v1 and copy.)  Instead, gcc silently ignore the assignment
>> so that v1 ends up still empty.  You say it's kind of snob of gcc? My
>> reaction exactly.  Well, my second reaction.  The first involved a lot
>> of cursing.
>>
>> Conclusion: I committed a few changes to keep the code portable.
>> Basically, one has to include a few valarray.resize() calls before
>> assignment. Unfortunately, we'll have to keep that in mind when we write
>> new valarray code.
>>
>> Luigi
>>
>>
>> P.S. About the MarketModel example: please whistle in my general
>> direction when it's done, so I can backport it to the 1.0 branch.
>> Thanks.
>>
>>
>>
>> --
>>
>> I'd never join any club that would have the likes of me as a member.
>> -- Groucho Marx
>>
>>
>>
>>    
>
>
>
>  


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev