casting

Expression with Math.random() always returns the same value

限于喜欢 提交于 2021-01-28 14:26:33
问题 For some reason this line of code int u=(int)Math.random()*6 + 1; will only return a 1 as a result. I found out that its just skipping the whole (int)Math.random()*6 and is only using the 1 as when I changed it to 2 it only returned 2. Anyone know What's happening? 回答1: The cast of Math.random() to int is occurring before the multiplication by 6. The cast operator is of higher precedence than * . The Math.random() method returns a random number between 0 (inclusive) and 1 (exclusive), so the

Newer version of GCC throws reinterpret_cast error

痞子三分冷 提交于 2021-01-28 14:09:48
问题 I am developing an embedded project (on STM32). I currently use GCC 4.9.2, but I would like to switch to newer version of my toolchain. Unfortunately my code which succesfully compiles on gcc 4.9.2, throws reinpreted_cast errors on version 6.2.0 or 7.2.0 and I have no idea why. It looks like that newer gcc sees some problems when casting int to pointer and back to int - which I think should be quite normal operation. Error message thrown: 1>STM32L4\CMSIS\stm32l4a6xx.h(1567,30): error :

Changing data type to float and rounding to 2 decimal digits

早过忘川 提交于 2021-01-28 12:39:29
问题 Tables: people(id, name) job (id, people_id, job_title, salary) Goal: Display each unique job, the total average salary (FLOAT and rounded to 2 decimal places) , the total people and the total salary (Float and rounded to 2 decimal places) and order by highest average salary. So the challenge is to keep the cast type as float while rounding it to 2 decimal places. I've gotten to where I've rounded it 2 decimal places but it's not float. I've gotten it to where it's float but I can't round it

Other way to cast varbit to int? And bigint?

半腔热情 提交于 2021-01-28 07:31:46
问题 This function is a workaround ... nothing with better performance ? CREATE or REPLACE FUNCTION varbit_to_int(v varbit) RETURNS int AS $f$ SELECT CASE bit_length(v) WHEN 1 THEN v::bit(1)::int WHEN 2 THEN v::bit(2)::int WHEN 3 THEN v::bit(3)::int ... WHEN 30 THEN v::bit(30)::int WHEN 31 THEN v::bit(31)::int WHEN 32 THEN v::bit(32)::int ELSE NULL::int END $f$ LANGUAGE SQL IMMUTABLE; Same problem for bigint: CREATE or replace FUNCTION varbit_to_bigint(p varbit) RETURNS bigint AS $f$ SELECT CASE

What are the names of type(myVar) and (type)myVar? Are they both bad practice?

时间秒杀一切 提交于 2021-01-28 07:08:44
问题 Almost a rehash of What's the difference between function(myVar) and (function)myVar? But I want to know: What is the name of these variants and are they 'bad'? type(myVar) is constructor like syntax, but for a basic type is it the same as doing a C-style cast which is considered bad in C++? (type)myVar this one certainly does seem to be a C-style cast and thus must be bad practice? I've seen some instances where people replace things like (int)a with int(a) citing that the C-style version is

Is struct of strings the same as array of strings?

陌路散爱 提交于 2021-01-28 03:56:28
问题 I'm dealing with code, that has multiple structures consisting of string -fields only. For example: struct record { string first; string second; string third; string Nth; }; These are currently all loaded from a database and stored in different map<string, somestruct> -- with a separate method for each. The methods are the same (except for the SQL-queries and field-names) -- the first field of each query becomes the map's index, and the subsequent ones form the structure's fields: void

Is it safe to cast an array to a struct with one array as member?

六眼飞鱼酱① 提交于 2021-01-28 02:03:51
问题 In the following setup typedef struct { unsigned char data[64]; } mystruct; int myfunc(mystruct* arg); // fills arg with data is it safe to call myfunc with a pointer to a 64 byte array? E.g. unsigned char buffer[64]; myfunc((mystruct*) buffer) In my concrete application I am using a JNI direct ByteBuffer which should be filled from myfunc . unsigned char* dbbuffer = (unsigned char*) (*env)->GetDirectBufferAddress(env, jbuffer); If the cast is not safe, I would have to create a mystruct ,

Trying to cast one object type into another in Python

眉间皱痕 提交于 2021-01-27 21:27:16
问题 I have this bit of code: const ON_Curve* curve = ...; const ON_NurbsCurve* nurb = ON_NurbsCurve::Cast( curve ); if( nurb ) { ON_Ellipse ellipse; double tolerance = model.m_settings.m_ModelUnitsAndTolerances.m_absolute_tolerance; bool rc = nurb->IsEllipse( 0, &ellipse, tolerance ); It casts a ON_NurbsCurve object to ON_Curve object. I am not quite sure if that's even possible in Python. I know i can take a string and cast it into an integer like: int("1"). I am not sure what is the right way

Convert pointer to int and back to typed object

点点圈 提交于 2021-01-27 17:33:49
问题 I need a simple way to convert a pointer to an int, and then back to the pointer. I need it as an int for portability, since an int is easier to carry around in my project than a pointer. Start player and return pointer: SoundPlayer* player = new FxPlayerTiny(); return (int)player; // this works, but is it correct? Stop player using pointer int: FxPlayerTiny* player = static_cast<FxPlayerTiny*>((int*)num); // this gives me an error FxPlayerTiny* player = (FxPlayerTiny*)((int*)obj); // this

Convert safely between uint8_t[8] & uint64_t via cast?

放肆的年华 提交于 2021-01-27 17:00:48
问题 The way I'm currently doing it (I'd prefer to get rid of the memcpy call): uint64_t integer; uint8_t string[8]; ... memcpy(&integer, &string, 8); //or swap the parameters Assuming integer array length to always be a multiple of 8 (64 bits total allocation) is a straight cast possible given compiler padding / alignment concerns? 回答1: There is absolutely no need to avoid or replace a memcpy() call if you're striving for optimization. Every modern optimizing compiler won't emit a call and