casting

When a long integer is cast into a short one, what happened?

纵然是瞬间 提交于 2019-12-19 19:47:39
问题 I use java to copy one long integer y to a short integer x: long y = 40002; short x = (short) y; System.out.println("x now equals " + x); The result is: x now equals -25534. I tried to figure out how 40002 was cast into -25534, but I failed. The 40002 corresponds to 1001 1100 0100 0010, the -25534 corresponds to 1110 0011 1011 1110. Can any friend tell me what happened in this process? Thanks a lot! 回答1: What you have done by casting a long to a short is a narrowing primitive conversion ,

What is (double (^)(int))foofoo

你说的曾经没有我的故事 提交于 2019-12-19 17:48:33
问题 There is an example on cdecl that goes (double (^)(int))foofoo means cast foofoo into block (int) returning double . What does it mean to cast foofoo into a "block" of int ? What does the symbol ^ exactly mean in this context. Usually it is bitwise XOR. 回答1: It's a GCC extension made by Apple, and implemented also in Clang. Blocks are small unnamed functions and that syntax is the type of a block. See Block Language Spec. 来源: https://stackoverflow.com/questions/3551999/what-is-double

Converting polygon coordinates from Double to Long for use with Clipper library

与世无争的帅哥 提交于 2019-12-19 17:41:32
问题 I have two polygons with their vertices stored as Double coordinates. I'd like to find the intersecting area of these polygons, so I'm looking at the Clipper library (C++ version). The problem is, Clipper only works with integer math (it uses the Long type). Is there a way I can safely transform both my polygons with the same scale factor, convert their coordinates to Longs, perform the Intersection algorithm with Clipper, and scale the resulting intersection polygon back down with the same

Converting polygon coordinates from Double to Long for use with Clipper library

陌路散爱 提交于 2019-12-19 17:41:31
问题 I have two polygons with their vertices stored as Double coordinates. I'd like to find the intersecting area of these polygons, so I'm looking at the Clipper library (C++ version). The problem is, Clipper only works with integer math (it uses the Long type). Is there a way I can safely transform both my polygons with the same scale factor, convert their coordinates to Longs, perform the Intersection algorithm with Clipper, and scale the resulting intersection polygon back down with the same

Trying to cast a boxed int to byte

风流意气都作罢 提交于 2019-12-19 17:39:41
问题 Code to illustrate : int i = 5; object obj = i; byte b = (byte)obj; // X When run, this generates a System.InvalidCastException ("Specified cast is not valid") at line "X". Doing a double cast works : byte b = (byte)(int)obj; I would have thought that you ought to be able to cast a boxed int (if it has a value in the range 0..255) to a byte. Can anyone shed any light on this ? (This is in .net 2.0, in case that matters). 回答1: The difference in behaviour you're seeing is the difference between

How to pointer-cast Foo** to const Foo** in C++

删除回忆录丶 提交于 2019-12-19 17:11:36
问题 I have class Fred { public: void inspect() const {}; void modify(){}; }; int main() { const Fred x = Fred(); Fred* p1; const Fred** q1 = reinterpret_cast<const Fred**>(&p1); *q1 = &x; p1->inspect(); p1->modify(); } How would it be possible to do the const Fred** q1 = &p1 via pointer-casting? (I have just been reading that this might be possible) Thank you for your answers. The const_cast works indeed for objects #include <iostream> #include <stdio.h> using namespace std; class Fred { int a;

Cast operation precedence in C#

余生颓废 提交于 2019-12-19 16:57:34
问题 Will the differences below matter significantly in C#? int a, b; double result; result = (double)a / b; result = a / (double)b; result = (double)a / (double)b; Which one do you use? 回答1: The cast will occur before the division. In your examples, it doesn't matter which one you do as if one operand is a double, the runtime will cast/convert the other to a double as well. This looks like a micro-optimization - not something worth worrying about or dealing with unless measurements show it is

How to check a double's bit pattern is 0x0 in a C++11 constexpr?

北慕城南 提交于 2019-12-19 16:13:20
问题 I want to check that a given double/float variable has the actual bit pattern 0x0. Don't ask why, it's used in a function in Qt ( qIsNull() ) that I'd like to be constexpr . The original code used a union: union { double d; int64_t i; } u; u.d = d; return u.i == 0; This doesn't work as a constexpr of course. The next try was with reinterpret_cast : return *reinterpret_cast<int64_t*>(&d) == 0; But while that works as a constexpr in GCC 4.7, it fails (rightfully, b/c of pointer manipulation) in

Why does CLng produce different results?

故事扮演 提交于 2019-12-19 15:47:27
问题 Here's a little gem directly from my VBE (MS Excel 2007 VBA): ?clng(150*0.85) 127 x = 150*0.85 ?clng(x) 128 Can anybody explain this behaviour? IMHO the first expression should yield 128 (.5 rounded to nearest even), or at least should both results be equal. 回答1: I think wqw is right, but I'll give the details. In the statement clng(150 * 0.85) , 150 * 0.85 is calculated in extended-precision: 150 = 1.001011 x 2^7 0.85 in double precision = 1

How to unbox a C# object to dynamic type

你。 提交于 2019-12-19 14:26:09
问题 I'm trying to do something like this: void someMethod(TypeA object) { ... } void someMethod(TypeB object) { ... } object getObject() { if (...) return new TypeA(); else return new TypeB(); } object obj = getObject(); (obj.GetType()) obj; // won't compile someMethod(obj); Obviously I'm confused here. I know I could make this work by just writing out a conditional statement -- if (obj.GetType() == typeof(TypeA)) obj = (TypeA)obj; else if (obj.GetType() == typeof(TypeB)) obj = (TypeB)obj; -- but