casting

PyBind11: boost::multiprecision::cpp_int to Python

早过忘川 提交于 2019-12-11 13:18:19
问题 I am interested in using pybind11 to optimize some Python computation using C++. The casting documentation doesn't make much sense to me and was wondering if anyone knew how to cast boost datatypes, specifically cpp_int, to a Python datatype so I can return computations. A simple example of what I'm trying to do would be factorials: #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <boost/multiprecision/cpp_int.hpp> using boost::multiprecision::cpp_int; namespace py = pybind11

Unchecked cast problem

ε祈祈猫儿з 提交于 2019-12-11 12:52:24
问题 Hey, I'm trying to implement the ShellSort algorithm and now I got a problem: warning: [unchecked] unchecked cast found : java.util.Vector required: java.util.Vector<java.lang.Object> Vector<Object> vcur = (Vector<Object>)currentCols[tcur]; Same with vtmp . I don't know where the problem is. It would be really great if you could help me. :) This is my code: public static Vector<Object> shellSort(Vector<Object> ul) { int lcount = ul.size(); int colcount = 4; // 2^x Vector[] currentCols = { ul

Java - How to avoid loss of precision during divide and cast to int?

江枫思渺然 提交于 2019-12-11 12:50:00
问题 I have a situation where I need to find out how many times an int goes into a decimal, but in certain cases, I'm losing precision. Here is the method: public int test(double decimalAmount, int divisor) { return (int) (decimalAmount/ (1d / divisor)); } The problem with this is if I pass in 1.2 as the decimal amount and 5 as the divisor, I get 5 instead of 6. How can I restrusture this so I know how many times 5 goes into the decimal amount as an int? 回答1: public int test(double decimalAmount,

Why am I getting InvalidCastException when casting object to integer?

血红的双手。 提交于 2019-12-11 12:43:16
问题 I had an odd problem, here's the setup: ASP.NET 3.5 app / MSSQLSERVER 2008 back-end. I called ExecuteScalar from my code, which returned an object, then I tried casting that object to int (i actually have a generic method T ConvertDBValue<T>(object o) {...} , but that's not as important). ExecuteScalar is hitting the following sproc: ... insert into ... select scope_identity() My primary key is an identity field, and returned 85. Next thing I got is InvalidCastException trying to cast 85 to

Why is there no String.valueOf(short a);

本小妞迷上赌 提交于 2019-12-11 12:32:27
问题 I had to cast short to a string and the only way I could do it was to concatenate an empty string with it, is this the right way? PS, why is there no String.valueOf(short a) ; 回答1: Because the creators of the JDK didn't think that it is needed. Given a short you can simply call String.valueOf(int) . Note that even Short.toString(short) is implemented as return Integer.toString((int)s, 10); 回答2: You can use short s = 5 String str = String.valueOf(s); // s is widened to int. or String str =

Forced necessity of dynamic_cast operator in C++

我只是一个虾纸丫 提交于 2019-12-11 12:19:32
问题 dynamic_cast is generally used when we have a base class pointer and want to downcast it to a derived class. For instance, class A { public: virtual void foo(); }; class B : public A { public: void foo(); }; main() { A* a = new B(); B* b = dynamic_cast<B*> (a); } But, the same can also be accomplised by using C-style cast: B* b = (B*)a; So, my question is what are the circumstances/cases where it becomes completely necessary to use this operator i.e. there is no other choice? 回答1: When you

Implementing simple functional-like paradigm in Java collections and type casting

久未见 提交于 2019-12-11 11:47:57
问题 I've recently started working in Scala and that was my first real contact with a functional paradigm. Although I'm a big fan of Java and, I admit, it lacks functional paradigm sometimes. That's why I recently started a a mini pet-project to see if such thing, to a degree, could be achieved in Java. I started with a simple modification of an array list, and this is what I have so far: An interface any collection needs to implement in order to offer applying functions to it's elements: public

Efficiently return IList<Interface> from List<T> (avoid casting from List<T> to List<I>)

蓝咒 提交于 2019-12-11 11:47:03
问题 I have the following code: public interface ISomeObject { IList<ISomeObject> Objects { get; } } public class SomeObject : ISomeObject { public SomeObject() { Objects = new List<SomeObject>(); } public List<SomeObject> Objects { get; set; } IList<ISomeObject> ISomeObject.Objects { get { // What to do here? // return Objects; // This doesn't work return Objects.Cast<ISomeObject>().ToList(); // Works, but creates a copy each time. } } SomeObject has a public property Objects that returns a List

printing pid with %d vs %ld, linux

狂风中的少年 提交于 2019-12-11 11:41:36
问题 I'm storing a series of pid s (i.e. Linux process ids) in an array of long s. I realize that a pid is not a long , but I have no choice in terms of using a different variable type. The issue I'm having occurs when I try and print the pid using printf . If I print the long that stores the pid using %ld , I get the wrong pids: 8435315771308 process_ancesto 8358006359962 bash 8353711392665 login 4294967297 init 0 swapper/0 However, if I print using %d (which generates a compiler warning), I get

Why does the compiler issue “warning: assignment makes integer from pointer without a cast”?

好久不见. 提交于 2019-12-11 11:35:36
问题 I don't understand why I keep getting an error when I try to compile it. I get an error saying fractions.c: In function "main": fractions.c:35:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:38:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:41:24: warning: assignment makes integer from pointer without a cast [enabled by default] fractions.c:44:24: warning: assignment makes integer from