design – Which is preferred: subclass double or create extension methods to test (relative) equality due to floating point differences?

I am writing financial calculation software using .NET C#, which needs to be blazingly fast. There is a lot of fractional math. So using decimal type is pretty much out of the question, given its poor speed relative to using double. But of course double has its problems testing for equality, with floating point rounding issues.

My options seem to be subclassing double and overriding ==, < and >; versus creating extension methods for double equivalent to these. My tendency is to go with the latter – less code to change and maybe it will be less confusing to others reading the code later? Is there another option? What are other good reasons to choose one over the other?

computer architecture – Is machine epsilon the largest relative error in representing a number as a floating point number?

Thanks for contributing an answer to Computer Science Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

android – Is it possible to have more than two Floating Action Buttons?

I’m struggling with something that should be pretty straight-forward, and now I don’t know if it is some undocumented limitation of the FAB element. I have three identical FABs in one screen inside an horizontal LinearLayout, but the third one doesn’t show its icon.

enter image description here

Here’s the code for that:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerInParent="true">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/gallery_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dialog_btn_spacing"
            android:layout_marginEnd="@dimen/dialog_btn_spacing"
            android:src="https://stackoverflow.com/@drawable/ic_photo_camera"
            app:backgroundTint="#FF5252"
            app:fabCustomSize="80dp" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/camera_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dialog_btn_spacing"
            android:layout_marginEnd="@dimen/dialog_btn_spacing"
            android:src="https://stackoverflow.com/@drawable/ic_photo_camera"
            app:backgroundTint="#4CAF50"
            app:fabCustomSize="80dp" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dialog_btn_spacing"
            android:layout_marginEnd="@dimen/dialog_btn_spacing"
            android:src="https://stackoverflow.com/@drawable/ic_photo_camera"
            app:backgroundTint="#21BFF3"
            app:fabCustomSize="80dp" />

    </LinearLayout>

Even more, if I add an additional one to the LinearLayout, the result is exactly the same, the fourth one not appearing at all. Is there some kind of limitations for the number of FABs to show? I’m not manipulating the FABs by code in any way, only adding a click listener to each of them. The icons are vector drawables directly imported from the Material icons library (and for the sake of this example, I put the same one for the three of them).

I’m not sure what the specific problem might be, but it is giving me a headache…

regex – Early Stages of floating point class template in C++

I’m currently designing a class template to represent scientific notation or a floating-point number system. There are currently 4 distinct types: BIN, DEC, OCT, & HEX. This could easily be expanded by the user.

This class utilizes some of the power of C++17 or greater such as scoped-enums, lambdas, string_view, and regular-expressions. With that being said, here is what the current state of my class looks like:

fpn.h

#pragma once

#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <map>
#include <optional>
#include <regex>
#include <string>
#include <utility>

namespace /*your namespace here*/ {

    enum class BaseTy {
        BIN = 2,
        OCT = 8,
        DEC = 10,
        HEX = 16
    };

    static const std::regex bin_regex(R"((-?(01)*).?((01)*))");
    static const std::regex oct_regex(R"((-?(0-7)*).?((0-7)*))");
    static const std::regex dec_regex(R"((-?(0-9)*).?((0-9)*))");
    static const std::regex hex_regex(R"((-?(0-9a-fA-F)*).?((0-9a-fA-F)*))");

    static const std::regex bin_exp_regex(R"(-?(01)*)");
    static const std::regex oct_exp_regex(R"(-?(0-7)*)");
    static const std::regex dec_exp_regex(R"(-?(0-9)*)");
    static const std::regex hex_exp_regex(R"(-?(0-9a-fA-F)*)");

    static const std::map<BaseTy, std::regex> ValidDigitSets = {
        {BaseTy::BIN, bin_regex},
        {BaseTy::OCT, oct_regex},
        {BaseTy::DEC, dec_regex},
        {BaseTy::HEX, hex_regex}
    };

    static const std::map<BaseTy, std::regex> ValidExponentSets = {
        {BaseTy::BIN, bin_exp_regex},
        {BaseTy::OCT, oct_exp_regex},
        {BaseTy::DEC, dec_exp_regex},
        {BaseTy::HEX, hex_exp_regex}
    };

    static const auto match = ()(const std::string& digits, const std::regex& regex) -> bool {
        std::smatch base_match;
        return std::regex_match(digits, base_match, regex);
    };

    template<BaseTy BASE = BaseTy::DEC>
    class Fpn {
    public:
        const uint16_t Base = static_cast<uint16_t>(BASE);

    public:
        std::string digits_{ "0" };
        int64_t integral_value_{ 0 };
        uint64_t decimal_value_{ 0 };
        int64_t exponent_{1};
        size_t decimal_location_{1};

        Fpn() = default;
        Fpn(const std::string_view digit_sequence, const std::string_view exponent = "") {
            std::cmatch digit_match;

            if (!std::regex_match(digit_sequence.data(), digit_match, ValidDigitSets.at(BASE))) {
                throw std::runtime_error("invalid digit sequence");
            }
            // Assert to make sure that the input has the correct character sets
            assert(
                (match(exponent.data(), ValidDigitSets.at(BASE))) &&
                 "invalid character sequence entered"
            );

            // if exponent is empty we can treat this as a value raised to the 1st power
            exponent_ = exponent.empty() ? 1 : std::stoi(exponent.data(), nullptr, static_cast<int>(BASE));
            if (exponent_ == 0) {
                digits_ = { "1" };
                integral_value_ = 1;
                decimal_value_ = 0;
                decimal_location_ = 1;
                return;
            }

            // Set the digits_ member value... 
            digits_ = digit_sequence.data();

            decimal_location_ = digit_match(1).length();
            if (digit_match(1).length() != 0) {
                integral_value_ = std::stoi(digit_match(1).str().c_str(), nullptr, static_cast<int>(BASE));
            }
            if (digit_match(2).length() != 0) {
                decimal_value_ = std::stoi(digit_match(2).str().c_str(), nullptr, static_cast<int>(BASE));
            }
        }

        template <typename OS>
        friend OS& operator << (OS& os, const Fpn& fpn) {
            return os << "digits{" << fpn.digits_ << "}nt"
                << "integral = " << fpn.integral_value_ << "nt"
                << "decimal = " << fpn.decimal_value_ << "nt"
                << "dec loc = " << fpn.decimal_location_ << "nt"
                << "exponent = " << fpn.exponent_ << "nn";
        }
    }; 

} // namespace /*your namespace*/

Here is the driver program that uses the above class:

main.cpp

#include "fpn.h"

int main() {
    try {            
        // using namespace /*your namespace*/;
        Fpn large("314195.2");
        Fpn small("1.24");
        Fpn sample("420");    

        std::cout << "large:nt" << large;
        std::cout << "small:nt" << small;
        std::cout << "sample:nt" << sample;
        std::cout << "bin:nt" << Fpn<BaseTy::BIN>("10.11");
        std::cout << "octal:nt" << Fpn<BaseTy::OCT>("17");
        std::cout << "hex:nt" << Fpn<BaseTy::HEX>("2A");        
    }
    catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Here is the generated output for simple basic cases:

Output

large:
        digits{314195.2}
        integral = 314195
        decimal = 2
        dec loc = 6
        exponent = 1

small:
        digits{1.24}
        integral = 1
        decimal = 24
        dec loc = 1
        exponent = 1

sample:
        digits{420}
        integral = 420
        decimal = 0
        dec loc = 3
        exponent = 1

bin:
        digits{10.11}
        integral = 2
        decimal = 3
        dec loc = 2
        exponent = 1

octal:
        digits{17}
        integral = 15
        decimal = 0
        dec loc = 2
        exponent = 1

hex:
        digits{2A}
        integral = 42
        decimal = 0
        dec loc = 2
        exponent = 1

I have not tested the exponent part just yet and I have not implemented any of the arithmetic or logical operators. However, the class appears to be working as expected.


The overall concept of this class is that it is not just fixed around a floating-point number that is fixed to a specific number system such as Decimal. The idea here is that it should be flexible for any number system… Log2 - Binary, Log8 - Octal, Log10 - Decimal, Log16 - Hexadecimal, LogX - Polynomial, etc.

There are four predefined types to this library. However, the user has the capability of extending this to any LogX number system by adding in their type to the enumerated class, updating the predefined regular expressions and their equivalent static const maps. The class itself should automatically handle the rest.


As for the structure of the representation of the numbering systems, the following applies for all types:

The left-hand side of the . is the integral part, the right-hand side of the . up to the ^ is the decimal part which is represented by the 1st string_view passed into the constructor, and everything to the right of the ^ is the exponent which is represented by the 2nd string_view passed into the constructor.

Here are some examples:

The class will calculate the integral and decimal values as well as the decimal point location or position. The exponent is handled separately in which I have yet to implement it to populate the internal member variable. The class is in its early stage so it is far from complete. This is now where I’m at within the design decisions…


From a design perspective, I am interested in your thoughts, concerns, and feedback.

  • Are there any code smells?
  • Does it seem to be modular and portable?
  • Is it generic and reusable?
  • Does it express intent and is it readable?
  • Does it exhibit the ability to be computationally efficient?
  • What kind of improvements can be made?
  • Are there any corner cases or gotchas that I’m missing or overlooking?
  • Even just personal feedback, opinions, or suggestions are welcomed.

These are the things I’d like to have answered or looked at before I continue to add any “functionality” to this class.


Edit

Note: Outside of the class I have two sets of regular expressions one for the sequence of digits and another for the exponent. Since this is in the early stages of development, I’m restricting exponents to be only integral values to keep things simple. In a future version, I may extend this to where the exponents could also be represented by floating-point values such as 12.34^5.6. In that case, then only the first set of regex would be needed where the second set might be considered redundant. This is just an additional note to the reader.

user research – Have studies shown people actually use floating “Back to Top” buttons?

I know there are “guidelines” out there for how to handle floating Back to Top buttons, but I haven’t found hard data or studies showing that users measurably prefer to use a Back to Top button rather than scroll.

Has anyone done any studies or know of analytics to show proportion of users leveraging Back to Top over scroll?

Asking because in my anecdotal design experience I’ve only ever added a floating Back to Top button at the insistence of a client without any usability data to back up the requirement. Now I need to introduce another feature that also performs best as a bottom-right floating button (Chat), but I would love to evidence studies demonstrating that Back to Top is worth nixing.

Validity of Algorithm for Testing Two Floating Point Numbers

This question is related to the epsilon- (or delta- if you prefer) test for floating point equality. But my question is not how to do it. Instead I have a related algorithm for testing equality, and I would like feedback as to what might be wrong with it.

The idea is to take the quotient of two floating point numbers and compare it to one. This eliminates the difficulty of choosing a value for epsilon. I have tested it extensively and am happy with the results. Here is a sample implementation in Java.

public class DoubleEquals
{
    public static final double  EPSILON = 1e-14;
    
    public static boolean equals( double param1, double param2 )
    {
        // Accounts for 0, +/-INFINITY;
        // if both values are NaN the result will be false
        boolean result  = param1 == param2;
        if ( !result )
        {
            double quot = param1 / param2;
            result = quot > 0 && (1 - quot) < EPSILON;
        }
        
        return result;
    }
}
```

IEEE 754 addition wrong result floating point numbers

I want to add two IEEE 754 numbers.
I followed the steps to add two 754 numbers. However the result it not correct.
Number 1:
S:0
E:01111111
M:11111111111111111111111

Number 2:
S:0
E:01111111
M:00000000000000000000000

Here is my calculation:

enter image description here

The site http://weitz.de/ieee/ gives this result:
S: 0
E: 10000000
M: 10000000000000000000000

in my calculation the mantissa is 01111…
Why?

applications – Floating action button not implementing Alert Dialog

I have a simple app that has a button which should display an alert dialog to the user on click but its not working.Below is the code for the button’s xml and java function code . Am new to this so any help will surely be apreciated..

Xml code for the button

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

Java code that handles the onclick event of that button

package com.example.login;

import android.app.AlertDialog;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;

import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

// 2. Chain together various setter methods to set the dialog characteristics
                builder.setMessage(R.string.dialog_message)
                        .setTitle(R.string.dialog_title);

// 3. Get the <code><a href="http://android.stackexchange.com/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a href="http://android.stackexchange.com/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>
                AlertDialog dialog = builder.create();
            }


        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The floating action fab is my button and alongside its methods of implementing the alert dialog box.
Thank You