How to cast double to double
How to cast double to double
How to convert double* to a double?
Any ideas for this typecasting problem?
Here’s what I am trying to do. This is not the actual code:
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You use the unary * operator to dereference a pointer. Dereferencing a pointer means extracting the value pointed to, to get a value of the original type.
edit 2: (deleted edit 1 as it was not relevant to your actual question, but was based on a miscommunication)
So, I’m assuming that your LinkedListCurrent is returning a void * pointing to some element in the linked list that you would like to update. In that case, you would need to do the following:
My explanation above is based on what I believe you are trying to do, based on the example snippet you posted and some guesses I’ve made about the interface. If you want a better explanation, or if one of my assumptions was bad, you might want to try posting some actual code, any error messages you are getting, and a more detailed explanation of what you are trying to do. Showing the interface to the linked list data type would help to provide some context for what your question is about.
edit 3: As pointed out in the comments, you probably shouldn’t be casting here anyhow; casts should be used as little as possible. You generally should use templated collection types, so your compiler can actually do typechecking for you. If you need to store heterogenous types in the same structure, they should generally share a superclass and have virtual methods to perform operations on them, and use dynamic_cast if you really need to cast a value to a particular type (as dynamic_cast can check at runtime that the type is correct).
Are double and double? interchangeable?
EDIT: What exactly is going on here?
double? d2 = d as double?
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
They’re not interchangeable as per your title.
What exactly is going on here?
Well, let’s go through it.
In the first line, you declare a local variable named d of type double. You assign the constant integer 5 to it. The compiler converts the constant integer 5 to the double 5.0 for you and generates code which assigns the value to the local.
In the second line you declare a local variable named d2 of type double?.
The portion of that which reads «d is double?» is evaluated as true, because d is known to be of type double. (When asked «x is y», if x is of a non-nullable type and y is the corresponding nullable type then the result is always true.)
The compiler knows this and therefore discards the alternative «(double?) null». Therefore the code generated is as though you’d said
This is generated by calling the constructor of double?, passing in d as the argument to the constructor, and a reference to local variable d2 as «this». So this becomes essentially:
That is exactly what is going on there. Does that all make sense?
C++ casting static two-dimensional double array to double**
I have such matrix in my program:
And I’d like to cast it to double** type.
I found almost working solution:
edit: You say that’s impossible. So I’ll change a problem a little bit. How can I pass two-dimensional double array as a parameter to a function? My function has prototype:
And it’s working fine with dynamically-allocated arrays. I doubt that only way to make it work is allocating new dynamical array and copy original static array one element by another.
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The notation double** refers to an array of pointers. You don’t have an array of pointers, you have an array of arrays of doubles.
You can’t just cast the array. You are going to have to create something like this:
Or you can use a loop:
The compiler actually creates an array of doubles as if you had written
the compiler replaces it by
A simple cast doesn’t create the variable rows above. Let me present other alternative (but equivalent) ways to define rows
As you can see, only the second size (i.e. 4 ) is necessary. In general, for multi-dimensional arrays, all sizes but the first are required. For this reason a 1-dimensional array
can be implicitly converted to a double*
Using this fact we can also write
Can a static_cast from double, assigned to double be optimized away?
I stumbled on a function that I think is unnecessary, and generally scares me:
Which is then used like this:
Is this ever any different from just doing this?:
The intention seems to be to just strip the double down to single precision. It smells like something written out of extreme paranoia.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The license to use greater precision is in C++ draft N4659 clause 8, paragraph 13:
The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby. 64
Footnote 64 says:
The cast and assignment operators must still perform their specific conversions as described in 8.4, 8.2.9 and 8.18.
In that function, marking the variable volatile tells the compiler that it cannot elide storing that value; that, in turn, means that it has to reduce the precision of the incoming value to match the type that it’s being stored in. So the hope is that this would force truncation.
And, no, writing a cast instead of calling that function is not the same, because the compiler (in its non-conforming mode) can skip the assignment to y if it determines that it can generate better code without storing the value, and it can skip the truncation as well. Keep in mind that the goal is to run floating-point calculations as fast as possible, and having to deal with niggling rules about reducing precision for intermediate values just slows things down.
In most cases, running flat-out by skipping intermediate truncations is what serious floating-point applications need. The rule requiring truncation on storage is more of a hope than a realistic requirement.
Java correct way convert/cast object to Double
I need to convert Object o to Double. Is the correct way to convert it to String first?
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
But it seems weird to me that you’re going from an Object to a Double. You should have a better idea what class of object you’re starting with before attempting a conversion. You might have a bit of a code quality problem there.
Note that this is a conversion, not casting.
If your Object represents a number, eg, such as an Integer, you can cast it to a Number then call the doubleValue() method.
You can’t cast an object to a Double if the object is not a Double.
Those methods give you a way of getting a Double instance from a String or double primitive. (Also not the constructors; read the documentation to see how they work) The object you are trying to convert naturally has to give you something that can be transformed into a double.
You can use the instanceof operator to test to see if it is a double prior to casting. You can then safely cast it to a double. In addition you can test it against other known types (e.g. Integer) and then coerce them into a double manually if desired.
In Java version prior to 1.7 you cannot cast object to primitive type
You can cast an Object to a Double just fine
Beware, it can throw a ClassCastException if your object isn’t a Double
You’ll have to manually «box» to the class Double when adding new items, and «unbox» to the primitive double by parsing and casting, doing something like this:
Источники информации:
- http://stackoverflow.com/questions/2241201/are-double-and-double-interchangeable
- http://stackoverflow.com/questions/15766382/c-casting-static-two-dimensional-double-array-to-double
- http://stackoverflow.com/questions/53118960/can-a-static-castfloat-from-double-assigned-to-double-be-optimized-away
- http://stackoverflow.com/questions/7503877/java-correct-way-convert-cast-object-to-double