tag:blogger.com,1999:blog-4855485143037509810.post5184469035678810077..comments2023-06-29T02:28:31.836-07:00Comments on Fortran in a C World: Ternary Operator ?Kyle Kanoshttp://www.blogger.com/profile/05322908974312681257noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-4855485143037509810.post-78492881417774448402017-07-06T11:59:16.941-07:002017-07-06T11:59:16.941-07:00This is a pretty good post. I think merge was real...This is a pretty good post. I think merge was really intended as a way to combine two arrays into a third, e.g. c=merge(a,b,a>0) means take the elements of a where they are greater than 0 and otherwise take the elements of b. For this, the name "merge" makes sense. <br /><br />It's kind of similar to the where() construct, e.g. where(a<=0)a=0, so the last argument is really a "mask", i.e. an array of logicals that. <br /><br />PS:<br />Counting Model (1-based) vs. Location Model (0-based)<br />I was a Fortran programmer for a long time and have recently added C/C++ to the repertoire. To help you come to terms with C's zero-indexing, think about this: would you start a number line axis at 1? No. Zero is the most natural "origin". The C array model is really just a memory layout model. You can get the address of a scalar 'a' in memory as '&a'. How do you refer to the memory block to the left/right of that? C chose that &a[1] is next door to the right, &a[-1] is next door to the left and &a[0] is the same as &a. When 'a' is an array, the same logic applies: &a[0] is the origin, &a[1] is the next element, etc. I have heard people say [] in C is a memory "offset" operator which is also used for array indexing. Did you know in most places in Europe, the ground floor is 0? It's kind of cool because then the below ground floor numbers are standardized: -1, -2, etc. instead of the "B1", "B2", "G", etc. <br /> Willhttps://www.blogger.com/profile/12332979824703457288noreply@blogger.comtag:blogger.com,1999:blog-4855485143037509810.post-81280803683109726452015-08-23T13:17:03.936-07:002015-08-23T13:17:03.936-07:00It should be noted that in contrast to C, both arg...It should be noted that in contrast to C, both arguments of merge function are calculated (or can be calculated, depending on a compiler), and you'll probably get a problem if one of them throws an exception even if this argument is not used according to the mask. So, the following call: merge(array(index), array(-index), index > 0) being innocent at first sight, can crash your program.Eugenehttps://www.blogger.com/profile/05028061815745977352noreply@blogger.comtag:blogger.com,1999:blog-4855485143037509810.post-6982512768467622292015-04-14T06:33:37.678-07:002015-04-14T06:33:37.678-07:00+MartinDiehl: Actually, I think it should just be ...+MartinDiehl: Actually, I think it should just be "else" in line 3 because we're testing for strictly greater than with merge and the C ternary operator. Using the "else if" there would eliminate equality (which should evaluate to false in the three cases, if they are to be equivalent).Kyle Kanoshttps://www.blogger.com/profile/05322908974312681257noreply@blogger.comtag:blogger.com,1999:blog-4855485143037509810.post-3549105078729345382015-04-13T01:32:33.148-07:002015-04-13T01:32:33.148-07:00Shouldn't line 3 read
"else if (a < b)...Shouldn't line 3 read<br />"else if (a < b) then" ?<br />For equality, the merge condition evaluates to falseAnonymoushttps://www.blogger.com/profile/09079686433932486516noreply@blogger.comtag:blogger.com,1999:blog-4855485143037509810.post-65700387268498183232013-12-24T19:26:45.640-08:002013-12-24T19:26:45.640-08:00pi=acos(-1.0) is superior to pi=4*atan(1.0) becaus...pi=acos(-1.0) is superior to pi=4*atan(1.0) because the latter loses two bits of precision.Anonymoushttps://www.blogger.com/profile/15564711622089021817noreply@blogger.com