char to decimal

  • Thread starter Dirk Bruere at NeoPax
  • Start date
J

Jeff Higgins

Funny, they could do all this for char, but not for boolean.

Recently I was translating a piece of C++ code to Java.
I'm wondering how others might make this translation.
Thanks, JSH.

double d2;
double d3;
double e;

C++ test:
int test = (int(d2 > e) << 1) + int(d3 > e);

Java test:
int t = d2 > e ? 1<<1 : 0;
int test = d3 > e ? t+1 : t;

switch(test)
{ case(0):
case(1):
case(2):
case(3):
}
 
M

Michael Wojcik

Andreas said:
I wasn't aware of any particular "English alphabet". There's
however the Latin alphabet, and a subset of it used in
English language. Now, I'm curious about an English sentence
using your particular samples of characters within words.

The ligatures and o-umlaut are relatively common in English
typography, particularly in books published before, say, 1950. Joshua
Cranmer already mentioned "coöperation" which (along with its lemmas)
is a prominent case of the latter.

I don't recall offhand seeing e-umlaut used in English sentences for
words that are not loan-words from other languages (though the
distinction between a loan-word and a "native" one in English is
rather vague anyway). But there may well be cases I'm not thinking of.

Whether those are part of "the English alphabet" is a question of
definition, since there is no generally-recognized authority to
provide a standard alphabet for English. At one time, "&" was part of
the alphabet, as taught in many English-speaking schools. The term
"ampersand" is a portmanteau of "and per se and", as the symbol was
then called "per se and" and it came at the end of the alphabet (so
recitation ended with "and per se and"). The letter "j" wasn't
well-established in the English alphabet until sometime in the 17th
century. And so on.
 
M

markspace

C++ test:
int test = (int(d2 > e) << 1) + int(d3 > e);

Java test:
int t = d2 > e ? 1<<1 : 0;
^^^^

Well, this is a 2. The Java coding conventions say to declare this as a
constant, I'm not sure that is valuable here though

int test = d3 > e ? t+1 : t;

t += d3 > e ? 1 : 0;

The whole thing could be done in one go, of course:

int test = ((d2 > e) ? 2 : 0) + ((d3 > e) ? 1 : 0);

switch(test)
{ case(0):
case(1):
case(2):
case(3):
}

Blech. So the whole point here seems to be to set the 1 bit if d2 > e,
and set the 0 bit if d2 > e. Why not just do that?

if( d2 > e ) {
if( d3 > e ) {
// case 0
} else {
// case 1
}
} else {
if( d3 > e ) {
// case 2
} else {
// case 3
}
}

I don't think there's more tests for the CPU in this case. In fact the
jump to implement the switch might be more expensive that just a simple
branch test. I'd have to know what is motivating the use of switch
instead of if-else.

Overall, I feel the last one, the if-else in Java, is the most clear and
maintainable. Given your apparent confusing trying to translate the C++
version literally, I think I'd say that promoting booleans to ints is a
bad idea, as it produces crap.
 
N

Nigel Wade

Recently I was translating a piece of C++ code to Java.
I'm wondering how others might make this translation.
Thanks, JSH.

double d2;
double d3;
double e;

C++ test:
int test = (int(d2 > e) << 1) + int(d3 > e);

Java test:
int t = d2 > e ? 1<<1 : 0;
int test = d3 > e ? t+1 : t;


If you intend to keep the obfuscation to a maximum, with shorthand
one-liners, then:

int test = ((d2>e)?2:0) | ((d3>e)?1:0);

Of course, the idiosyncrasies of C/C++ do permit a higher degree of
obfuscation for the intent of the code.
 
M

markspace

Overall, I feel the last one, the if-else in Java, is the most clear and
maintainable. Given your apparent confusing trying to translate the C++
version literally, I think I'd say that promoting booleans to ints is a
bad idea, as it produces crap.


And my confusion too, since I think Patricia has the control flow correct.
 
L

Lew

I wasn't aware of any particular "English alphabet". There's
however the Latin alphabet, and a subset of it used in
English language. Now, I'm curious about an English sentence
using your particular samples of characters within words.

Oh, and please don't make it a trivial one, like
"'Ö' is not an English letter."

I'll give you references to a couple. Make your own sentences if the ones
therein fail to suit.

http://en.wikipedia.org/wiki/Noösphere

For the general use of the umlaut, or trema, in English, see
http://en.wikipedia.org/wiki/Trema_(diacritic)
"For example, in the spelling coöperate, the diaeresis reminds the reader that
the word has four syllables co-op-er-ate, not three, *coop-er-ate. This usage
is uncommon in English, and is always optional ..."

The ligatures 'æ' and 'Å“', and others such as 'ij', 'ff', and 'ï¬', appear where
their corresponding digraph appears in the conventional spelling, such as in
the word "diæresis".

http://en.wikipedia.org/wiki/Typographic_ligature
'In modern English orthography Æ is not considered an independent letter but a
spelling variant, for example: "encyclopædia" versus "encyclopaedia" or
"encyclopedia".

'Æ comes from Medieval Latin, where it was an optional ligature in some words,
for example, "Æneas". It is still found as a variant in English and French,
but the trend has recently been towards printing the A and E separately.'

So here's a sentence for you,

"Those with access to an encyclopædia can find in the noösphere that seven
bits do not suffice to represent all the different letters used in English."

Oh, and to use onomatopœia,
http://en.wikipedia.org/wiki/Onomatopœia

Pllpptttthththtttppppplkttthhh!
 
L

Lew

The ligatures and o-umlaut are relatively common in English
typography, particularly in books published before, say, 1950. Joshua
Cranmer already mentioned "coöperation" which (along with its lemmas)
is a prominent case of the latter.

I don't recall offhand seeing e-umlaut used in English sentences for
words that are not loan-words from other languages (though the
distinction between a loan-word and a "native" one in English is
rather vague anyway). But there may well be cases I'm not thinking of.

The preëminence of the e-umlaut has faded.
 
D

Daniele Futtorovic

Recently I was translating a piece of C++ code to Java.
I'm wondering how others might make this translation.
Thanks, JSH.

double d2;
double d3;
double e;

C++ test:
int test = (int(d2 > e) << 1) + int(d3 > e);

Java test:

int gt( int t1, int i2 ) { return i1 > i2 ? 1 : 0 }

void test() {
int test = (gt(d2, e) << 1) + gt(d3, e);
}
 
D

Daniele Futtorovic

Joshua
Cranmer already mentioned "coöperation" which (along with its lemmas)
is a prominent case of the latter.

Didn't know that, but it's preposterous. Trying to pronounce that makes
me sound like a Saxon. ;)
 
J

Jeff Higgins

Recently I was translating a piece of C++ code to Java.
I'm wondering how others might make this translation.
Thanks, JSH.

double d2;
double d3;
double e;

C++ test:
int test = (int(d2 > e) << 1) + int(d3 > e);

Java test:
int t = d2 > e ? 1<<1 : 0;
int test = d3 > e ? t+1 : t;

switch(test)
{ case(0):
case(1):
case(2):
case(3):
}
Thanks for the responses.
Yes, the nested if/else seems more clear in the above short example.
Now I wonder about the original author's motivation for
the use of switch statement. Code clarity, optimization, idiomatics?
Somehow the code seems easier for me to read with the use of the
switch simply because of the length of the intervening code.

void curve4_div::recursive_bezier(double x1, double y1, double x2,
double y2, double x3, double y3, double x4, double y4, unsigned level) {

if (level > curve_recursion_limit) {
return;
}

// Calculate all the mid-points of the line segments
//----------------------
double x12 = (x1 + x2) / 2;
double y12 = (y1 + y2) / 2;
double x23 = (x2 + x3) / 2;
double y23 = (y2 + y3) / 2;
double x34 = (x3 + x4) / 2;
double y34 = (y3 + y4) / 2;
double x123 = (x12 + x23) / 2;
double y123 = (y12 + y23) / 2;
double x234 = (x23 + x34) / 2;
double y234 = (y23 + y34) / 2;
double x1234 = (x123 + x234) / 2;
double y1234 = (y123 + y234) / 2;

// Try to approximate the full cubic curve by a single straight line
//------------------
double dx = x4 - x1;
double dy = y4 - y1;

double d2 = fabs(((x2 - x4) * dy - (y2 - y4) * dx));
double d3 = fabs(((x3 - x4) * dy - (y3 - y4) * dx));
double da1, da2, k;

switch ((int(d2 > curve_collinearity_epsilon) << 1) +
int(d3 > curve_collinearity_epsilon)) {
case 0:
// All collinear OR p1==p4
//----------------------
k = dx * dx + dy * dy;
if (k == 0) {
d2 = calc_sq_distance(x1, y1, x2, y2);
d3 = calc_sq_distance(x4, y4, x3, y3);
} else {
k = 1 / k;
da1 = x2 - x1;
da2 = y2 - y1;
d2 = k * (da1 * dx + da2 * dy);
da1 = x3 - x1;
da2 = y3 - y1;
d3 = k * (da1 * dx + da2 * dy);
if (d2 > 0 && d2 < 1 && d3 > 0 && d3 < 1) {
// Simple collinear case, 1---2---3---4
// We can leave just two endpoints
return;
}
if (d2 <= 0)
d2 = calc_sq_distance(x2, y2, x1, y1);
else if (d2 >= 1)
d2 = calc_sq_distance(x2, y2, x4, y4);
else
d2 = calc_sq_distance(x2, y2, x1 + d2 * dx, y1 + d2 * dy);

if (d3 <= 0)
d3 = calc_sq_distance(x3, y3, x1, y1);
else if (d3 >= 1)
d3 = calc_sq_distance(x3, y3, x4, y4);
else
d3 = calc_sq_distance(x3, y3, x1 + d3 * dx, y1 + d3 * dy);
}
if (d2 > d3) {
if (d2 < m_distance_tolerance_square) {
m_points.add(point_d(x2, y2));
return;
}
} else {
if (d3 < m_distance_tolerance_square) {
m_points.add(point_d(x3, y3));
return;
}
}
break;

case 1:
// p1,p2,p4 are collinear, p3 is significant
//----------------------
if (d3 * d3 <= m_distance_tolerance_square * (dx * dx + dy * dy)) {
if (m_angle_tolerance < curve_angle_tolerance_epsilon) {
m_points.add(point_d(x23, y23));
return;
}

// Angle Condition
//----------------------
da1 = fabs(atan2(y4 - y3, x4 - x3) - atan2(y3 - y2, x3 - x2));
if (da1 >= pi)
da1 = 2 * pi - da1;

if (da1 < m_angle_tolerance) {
m_points.add(point_d(x2, y2));
m_points.add(point_d(x3, y3));
return;
}

if (m_cusp_limit != 0.0) {
if (da1 > m_cusp_limit) {
m_points.add(point_d(x3, y3));
return;
}
}
}
break;

case 2:
// p1,p3,p4 are collinear, p2 is significant
//----------------------
if (d2 * d2 <= m_distance_tolerance_square * (dx * dx + dy * dy)) {
if (m_angle_tolerance < curve_angle_tolerance_epsilon) {
m_points.add(point_d(x23, y23));
return;
}

// Angle Condition
//----------------------
da1 = fabs(atan2(y3 - y2, x3 - x2) - atan2(y2 - y1, x2 - x1));
if (da1 >= pi)
da1 = 2 * pi - da1;

if (da1 < m_angle_tolerance) {
m_points.add(point_d(x2, y2));
m_points.add(point_d(x3, y3));
return;
}

if (m_cusp_limit != 0.0) {
if (da1 > m_cusp_limit) {
m_points.add(point_d(x2, y2));
return;
}
}
}
break;

case 3:
// Regular case
//-----------------
if ((d2 + d3) * (d2 + d3) <= m_distance_tolerance_square * (dx * dx
+ dy * dy)) {
// If the curvature doesn't exceed the distance_tolerance value
// we tend to finish subdivisions.
//----------------------
if (m_angle_tolerance < curve_angle_tolerance_epsilon) {
m_points.add(point_d(x23, y23));
return;
}

// Angle & Cusp Condition
//----------------------
k = atan2(y3 - y2, x3 - x2);
da1 = fabs(k - atan2(y2 - y1, x2 - x1));
da2 = fabs(atan2(y4 - y3, x4 - x3) - k);
if (da1 >= pi)
da1 = 2 * pi - da1;
if (da2 >= pi)
da2 = 2 * pi - da2;

if (da1 + da2 < m_angle_tolerance) {
// Finally we can stop the recursion
//----------------------
m_points.add(point_d(x23, y23));
return;
}

if (m_cusp_limit != 0.0) {
if (da1 > m_cusp_limit) {
m_points.add(point_d(x2, y2));
return;
}

if (da2 > m_cusp_limit) {
m_points.add(point_d(x3, y3));
return;
}
}
}
break;
}

// Continue subdivision
//----------------------
recursive_bezier(x1, y1, x12, y12, x123, y123, x1234, y1234, level + 1);
recursive_bezier(x1234, y1234, x234, y234, x34, y34, x4, y4, level + 1);
}
 
D

Dirk Bruere at NeoPax

markspace wrote:
Dirk Bruere at NeoPax wrote:

I need it to match the packet i/f [?] specs designed by somemone else
that
requires text characters be sent as decimal ascii [sic]


That's a really odd requirement. Your spec might mean just regular
text. Not
ascii text, as in the character 9 followed by the character 7, but
just 'a' as
a literal 97 byte value.

Just saying.

What value does that spec indicate to transmit, say, the characters 'æ'
or 'À', Dirk?

English alphabet only

The English alphabet includes 'æ', 'ë', 'ö', 'œ' and other such symbols
not included in ASCII.

Not in my part of the tech world it doesn't.
We don't do Old English Runes either.
 
L

Lew

Didn't know that, but it's preposterous. Trying to pronounce that makes me
sound like a Saxon. ;)

Trying to pronounce what is preposterous?

The word "coöperation" is pronounced the way you've always pronounced it, so I
cannot figure out what you mean.
 
L

Lew

On 05/05/2011 22:10, Lew wrote:
markspace wrote:
Dirk Bruere at NeoPax wrote:

I need it to match the packet i/f [?] specs designed by somemone else
that
requires text characters be sent as decimal ascii [sic]


That's a really odd requirement. Your spec might mean just regular
text. Not
ascii text, as in the character 9 followed by the character 7, but
just 'a' as
a literal 97 byte value.

Just saying.

What value does that spec indicate to transmit, say, the characters 'æ'
or 'À', Dirk?

English alphabet only

The English alphabet includes 'æ', 'ë', 'ö', 'œ' and other such symbols
not included in ASCII.

Not in my part of the tech world it doesn't.
We don't do Old English Runes either.

Excuse me, but what does the "tech world" have to do with what is and is not
in English? Those letters are part of English orthography. If your "part of
the tech world" doesn't recognize that fact, then the lack of recognition is
an error. Ignoring reality doesn't change reality.
 
D

Daniele Futtorovic

Trying to pronounce what is preposterous?

The word "coöperation" is pronounced the way you've always pronounced
it, so I cannot figure out what you mean.

You won't get it if you don't speak German and know how they speak it it
the further south-east. :)
 
L

Lew

Daniele said:
You won't get it if you don't speak German and know how they speak it it the
further south-east. :)

Is that what's called High German? I am rather fascinated by languages, and
of course Saxon is one of the root languages of English.

I suppose there's no way to convey the humor of an accent via a text medium,
but I can imagine by reference to cognate American phenomena.

One fascinating factoid: Not only do linguistically isolated communities,
such as English speakers in the American Ozarks and Farsi speakers in the
mountains of Afghanistan, retain linguistic patterns and accents longer than
their cosmopolitan (often coastal) counterparts, the culture of the
linguistically mixed often disparages that of their isolated brethren as
"hillbilly" or the equivalent, and just as unfairly.

Thank you for a fascinating glimpse into German language and culture.
 
D

Daniele Futtorovic

Is that what's called High German? I am rather fascinated by languages,
and of course Saxon is one of the root languages of English.

Saxon in this case being a German accent.

I'm terribly embarrassed about this, actually. I know a few Saxons and
they're generally a great people. But I always have to refrain myself
from laughing when I hear them talk. I really hate myself for that.

I suppose there's no way to convey the humor of an accent via a text
medium, but I can imagine by reference to cognate American phenomena.

One fascinating factoid: Not only do linguistically isolated
communities, such as English speakers in the American Ozarks and Farsi
speakers in the mountains of Afghanistan, retain linguistic patterns and
accents longer than their cosmopolitan (often coastal) counterparts, the
culture of the linguistically mixed often disparages that of their
isolated brethren as "hillbilly" or the equivalent, and just as unfairly.

Same thing in biology. Isolated populations (e.g. insular ones) will
generally retain traits longer than the population they split off from.
Less "evolutionary pressure", so to speak.
 
A

Andreas Leitgeb

Daniele Futtorovic said:
You won't get it if you don't speak German and know how they speak it it
the further south-east. :)

@Lew: I think, Daniele refers to that "ö" in German is not just an "o"
separated from the previous vowel, but really a different sound.

@Daniele: Wie sachen's denn die Sachsen? :)

@all: thanks for your elaborations on English use of non-ASCII letters.
 
D

Dirk Bruere at NeoPax

On 05/06/2011 05:45 AM, Dirk Bruere at NeoPax wrote:
On 05/05/2011 22:10, Lew wrote:
markspace wrote:
Dirk Bruere at NeoPax wrote:

I need it to match the packet i/f [?] specs designed by somemone
else
that
requires text characters be sent as decimal ascii [sic]


That's a really odd requirement. Your spec might mean just regular
text. Not
ascii text, as in the character 9 followed by the character 7, but
just 'a' as
a literal 97 byte value.

Just saying.

What value does that spec indicate to transmit, say, the characters
'æ'
or 'À', Dirk?

English alphabet only

The English alphabet includes 'æ', 'ë', 'ö', 'œ' and other such symbols
not included in ASCII.

Not in my part of the tech world it doesn't.
We don't do Old English Runes either.

Excuse me, but what does the "tech world" have to do with what is and is
not in English? Those letters are part of English orthography. If your
"part of the tech world" doesn't recognize that fact, then the lack of
recognition is an error. Ignoring reality doesn't change reality.

Well, as I said elsewhere in the thread:
"I need it to match the packet i/f specs designed by somemone else that
requires text characters be sent as decimal ascii "

If it's not in ascii its not needed.

Ignoring what I wrote doesn't change reality either.
 
A

Andreas Leitgeb

Lew said:
Those letters are part of English orthography.

Hmm, some of the Wikipedia-pages you gave us were quite explicit that
these things were rather a matter of typography than of orthography.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top