|
I just tried something similar in C++ and it produces the correct results. Now I am
|
|
|
|
|
I finished my new tests and updated my original post to include the results. I stand by my original premise. There is something uniquely, and inexplicably, terrible about float ->double conversions.
The test randomly generated 1 million numbers with between one and six digits of precision. Of the six possible floating-point conversions, only the ToString for float ->double ever differed from the "expected" text. It did so a whopping 750,741 times. I beleive this is what they call "statistically significant"
|
|
|
|
|
OK, this is sadly nearing obsessional I've gone through the effort of decoding every dang bit in the IEEE 754 formats.
Near as I can tell, the float ->double conversion is doing absolutely what I would expect of it. Though, it is still yielding a result that has the appearance of being worse.
I'm currently baffled...maybe double.ToString() is the culprit? Maybe its something else entirely?
I'm going to give this a whole lot more thought tomorrow...at a decent hour. Though, since it has no practical impact on anything I'm actually doing, I should probably let it go. Regrettably, intellectual curiosity has a firm hold of me at this point
Below you'll find the output from my latest program, where I enter the text "123.456". "Single" / "Double" are conversions from the results of decimal .Parse . "Single (direct)" / "Double (direct)" are the results of float .Parse /double .Parse . The remainder are the indicated conversions of the results of a float .Parse .
Single: Sign=0, Exponent=6 (10000101), Significand=7793017 (11101101110100101111001)
123.456
Single (direct): Sign=0, Exponent=6 (10000101), Significand=7793017 (11101101110100101111001)
123.456
Double: Sign=0, Exponent=6 (10000000101), Significand=4183844053827191 (1110110111010010111100011010100111111011111001110111)
123.456
Double (direct): Sign=0, Exponent=6 (10000000101), Significand=4183844144021504 (1110110111010010111100100000000000000000000000000000)
123.456001281738
float->double: Sign=0, Exponent=6 (10000000101), Significand=4183844144021504 (1110110111010010111100100000000000000000000000000000)
123.456001281738
float->decimal->double: Sign=0, Exponent=6 (10000000101), Significand=4183844053827191 (1110110111010010111100011010100111111011111001110111)
123.456
After this much effort, I guess I'll eventually be forced to write an article on every useless bit of trivia I can find about all of these formats
|
|
|
|
|
To add to Richard's comments, you might get a surprise if you print the values to more digits of precision. There are literally billions of distinct numbers which print as 123.456 to 3 decimal places.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Based on my misinterpretation of the IEEE 754 specification, which I foolishly shared in a response to Richard, it seems today is a day full of surprises for me
That said, I would not expect converting from IEEE 754 binary32 to binary64 would make things apparently worse. I tried a bunch of different values. Of the six possible floating point conversions this one consistently yields the worst apparent outcome.
If anything, I would have expected converting binary64 to binary32 to have the worst apparent outcome.
|
|
|
|
|
I conducted some more rigorous experiments. This explanation doesn't match the experimental evidence.
I now feel somewhat certain, based on evidence from a million randomly generated numbers, that there is something uniquely, and inexplicably, terrible about float ->double conversions.
I qualify it with "somewhat", because I refuse to be completely wrong, about the same thing, twice in a single day
|
|
|
|
|
This discussion from 2011 looks relevant:
What you get in the more precise representation (past a certain point) is just garbage. If you were to cast it back to a float FROM a double, you would have the exact same precision as you did before.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So i have a bunch of files in a folder inside of my project. I need all the names of those files to go in an array so i can choose a random one with a randol index. I searched already on google but i could only find solutions for when you are trying to reach a directory that is not inside of the current project.
|
|
|
|
|
|
Perhaps I want something that is impossible... but thought I would ask anyway because I cannot find a way to do this... and it seems like it should be possible.
I want to force the definition of a method in all subclasses without that method being virtual. (I don't want a virtual table and lookup to be necessary). Normally one would do this with an interface. The problem is that I also need to define a property with an explicit definition. Normally that is done with an abstract class (which is the only reason I need to define one) An example is illustrated below. The problem I am running into is that if I inherit the interface in the abstract class, none of the sub-classes require the "NeededFunction" to be implemented, which is counter to what I want. If I don't implement the method in the abstract class however, it gives me an error. Is there a way to do this... basically I want the derived classes to implement the "NeededFunction" rather than the abstract class (without them being virtual for performance reasons). I know I can have every derived class implement the interface instead but that seems needlessly redundant.
interface IMyFunctionInterface
{
void NeededFunction(object state);
}
internal abstract class MyBaseImplementation : IMyFunctionInterface
{
internal MyRelevantProperty { get; protected set; }
public void NeededFunction(object state)
{
}
}
internal class Implementation1 : MyBaseImplementation
{
public void NeededFunction(object state)...
}
modified 27-Feb-19 13:05pm.
|
|
|
|
|
AFAIK, the only way to force the derived class to implement the method is to mark it as abstract . However, that would still be a virtual method.
internal abstract class MyBaseImplementation : IMyFunctionInterface
{
internal Foo MyRelevantProperty { get; protected set; }
public abstract void NeededFunction(object state);
}
internal class Implementation1: MyBaseImplementation
{
public sealed override void NeededFunction(object state) { ... }
}
If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.
pr1mem0ver wrote: without them being virtual for performance reasons
Do you have any real performance data from your application to justify that requirement? Particularly since method calls on an interface will always be virtual anyway...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.
Yeah... I have seen this suggestion elsewhere (older posts) but I get an error that the abstract class doesn't implement the method required for the interface. Is this error specific to newer versions of .NET?
|
|
|
|
|
The "doesn't implement the method" error appears when you declare that your class implements the interface, but it doesn't provide a declaration for one or more members defined by the interface.
This has always been the case since .NET 1.0, and has not changed with newer versions.
There will be a semi-related change with C# 8, which will only be available to .NET Core projects. But not in the way you've described:
C# 8: Default Interface Methods Implementation - CodeJourney.net[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You want to "force" a method override, but do not want to implement the interface ... because it "seems redundant".
How can it be "redundant" if you want to "force" the implementation of the method? You still need to add the method to the derived class and "know" if you didn't; so which part is "redundant"?
In any event, you can use "new" to override a non-virtual method and complain about it using reflection.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
You are not following what I mean by redundant. I mean redundant in that I have to add the Interface to every single derived class rather than just the base class.
You also don't seem to get that part of the point of doing things this way is because conceptually it is more robust. This is one use of CRTP's in C++.
|
|
|
|
|
Hello,
my name is Nikol, and currently, I am learning to code with C#. I am solving a problem, and I am getting stuck on this step. I read a sequence of numbers and a special "bomb" number and its power. For example if the sequence is:
{1, 2, 2, 4, 2, 2, 2, 9} with "bomb" number 4 and power: 3
we have to find the index of the bomb number (3) and remove 'power' count numbers from its left (remove 3 numbers from its left). We should receive the sequence:
{2, 2, 2, 9}.
Can you see my code and give me some advice? Are all cases covered? The else which I added is for the cases when the power is bigger than the numbers we have to its left. Btw, I set the elements to 0 because at the end I need the sum of the elements in the list and it doesn't matter. Thank you in advance! https://pastebin.com/mPeg5Xku
|
|
|
|
|
Some points to consider:
- You don't display any instructions to the user. Will they know what they're expected to type in?
- What happens if the user types in something which isn't an integer? Eg:
1 2 wibble - What happens if the user only types a single number on the second line?
- You're skipping the last number in the list (
i < numbers.Count - 1 ). What if that's the "bomb" number? - You're not actually removing the numbers from the list; you're just setting them to
0 . Depending on the wording of the assignment, that may not be correct. - Your
if..else block has almost identical code. The only difference is the starting value of j . Can you combine the branches?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I export my data from a DGV and align them with a space delimiter and it is working good for me. i also export the headers before the cells values. the thing that i want to do but i can not is aligning and adjusting the cells with headers to see them as a table that its members are separated by a delimiter (here space). my means is i want to see cells values right below the headers (if they have smaller or equal lengths) and headers text right on the top of the cells values if header length is less than cells values lengths), in the other word, Keeping cells values and their related headers together.(i am using Microsoft.Office.Interop.Word.)
thanks in advance.
What i have been tried:
int RowsCount = DataTable.Rows.Count;
int ColumnsCount = DataTable.Columns.Count;
var newline = System.Environment.NewLine;
var delimiter = " ";
var copied= new StringBuilder();
List<int> clmnLocation = new List<int>() {};
string header = "";
for (int c = 0; c < DataTable.Columns.Count; c++)
{
header = header + Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ", "") + delimiter;
clmnLocation.Add(header.Length);
}
for (int c = 0; c < DataTable.Columns.Count; c++) copied.Append(Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ","") + delimiter);
clipboard_string.Append(newline);
for (int i = 0; i <RowsCount; i++)
{
for (int j = 0; j <ColumnsCount; j++)
{
if (j > 0)
copied.Append(DataTable.Rows[i][j].ToString().PadRight(clmnLocation[j]) + delimiter);
else
copied.Append(DataTable.Rows[i][j].ToString() + delimiter);
}
copied.Append(newline);
}
WordDoc.Content.Text = copied.ToString();
|
|
|
|
|
If you want "headers" aligned in a (mono-spaced) text file, you will first need to determine the "maximum length / width" of each value in each column (plus a space for a column separator, if not "tabbing").
Besides aligning the headers, you will also need to align the columns.
Easier to just open in Excel as a CSV if you want to "look" at it.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
|
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();
The textbox doesn't scroll.
Can someone tell me what I am missing?
|
|
|
|
|
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting this on everything. You don't need it as it's already implied.
|
|
|
|
|
|
Dave Kreskowiak wrote: Stop putting this on everything. That was my idea to remind him where the control lives (he was creating a new form and lost the plot)
Implied can be confusing the this newbie, thought it might help.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|