-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,196 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
<Application x:Class="FloatingPointPlayground.App" | ||
<!-- | ||
Copyright (C) 2019 Dwscdv3 <[email protected]> | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<Application x:Class="FloatingPointPlayground.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:FloatingPointPlayground" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
/** | ||
* Copyright (C) 2019 Dwscdv3 <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using System.Windows; | ||
|
||
namespace FloatingPointPlayground | ||
{ | ||
/// <summary> | ||
/// App.xaml 的交互逻辑 | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
public partial class App : Application { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,148 @@ | ||
<Window x:Class="FloatingPointPlayground.MainWindow" | ||
<!-- | ||
Copyright (C) 2019 Dwscdv3 <[email protected]> | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<Window x:Class="FloatingPointPlayground.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:FloatingPointPlayground" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="450" Width="800"> | ||
<Grid> | ||
|
||
</Grid> | ||
xmlns:user="clr-namespace:FloatingPointPlayground.UserControls" | ||
xmlns:shape="clr-namespace:FloatingPointPlayground.Shapes" | ||
Title="Floating-point playground" | ||
Width="800" MinWidth="800" MinHeight="300" SizeToContent="Height"> | ||
<StackPanel x:Name="layoutRoot" Margin="10"> | ||
<VisualStateManager.VisualStateGroups> | ||
<VisualStateGroup x:Name="FloatInputGroup"> | ||
<VisualStateGroup.Transitions> | ||
<VisualTransition GeneratedDuration="0:0:0.1" To="ParseError"/> | ||
<VisualTransition From="ParseError" GeneratedDuration="0:0:0.1"/> | ||
</VisualStateGroup.Transitions> | ||
<VisualState x:Name="Default"/> | ||
<VisualState x:Name="ParseError"> | ||
<Storyboard> | ||
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="txtFloat"> | ||
<EasingColorKeyFrame KeyTime="0" Value="#FFFF7F7F"/> | ||
</ColorAnimationUsingKeyFrames> | ||
</Storyboard> | ||
</VisualState> | ||
</VisualStateGroup> | ||
</VisualStateManager.VisualStateGroups> | ||
<StackPanel HorizontalAlignment="Left" Margin="10,0"> | ||
<TextBlock Text="Single-Precision" FontSize="36" Foreground="CadetBlue"/> | ||
<Rectangle StrokeThickness="3" Stroke="CadetBlue" Margin="0,5,0,0"/> | ||
</StackPanel> | ||
<StackPanel Margin="10,10"> | ||
<Viewbox VerticalAlignment="Center"> | ||
<StackPanel x:Name="flippersContainer" Orientation="Horizontal" user:BitFlipper.Click="OnBitFlipperClick"> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
<user:BitFlipper/> | ||
</StackPanel> | ||
</Viewbox> | ||
<Grid Height="40"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition/> | ||
<RowDefinition/> | ||
</Grid.RowDefinitions> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="0.5*"/> | ||
<ColumnDefinition Width="0.5*"/> | ||
<ColumnDefinition Width="0.5*"/> | ||
<ColumnDefinition Width="7*"/> | ||
<ColumnDefinition Width="1*"/> | ||
<ColumnDefinition Width="22*"/> | ||
<ColumnDefinition Width="0.5*"/> | ||
</Grid.ColumnDefinitions> | ||
<Grid.Resources> | ||
<Style TargetType="TextBlock"> | ||
<Setter Property="HorizontalAlignment" Value="Center"/> | ||
</Style> | ||
</Grid.Resources> | ||
<Rectangle Grid.Column="1" HorizontalAlignment="Left" Stroke="Black"/> | ||
<shape:Bracket Grid.Column="3"/> | ||
<shape:Bracket Grid.Column="5"/> | ||
<TextBlock Text="Sign" Grid.Row="1" Grid.ColumnSpan="2"/> | ||
<TextBlock Text="Exponent" Grid.Row="1" Grid.Column="3"/> | ||
<TextBlock Text="Fraction" Grid.Row="1" Grid.Column="5"/> | ||
</Grid> | ||
</StackPanel> | ||
<Grid HorizontalAlignment="Center" Margin="0,0,0,10"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition/> | ||
<RowDefinition/> | ||
<RowDefinition/> | ||
</Grid.RowDefinitions> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition MinWidth="130"/> | ||
<ColumnDefinition/> | ||
</Grid.ColumnDefinitions> | ||
<Grid.Resources> | ||
<Style TargetType="TextBlock"> | ||
<Style.Triggers> | ||
<Trigger Property="Grid.Row" Value="0"> | ||
<Setter Property="FontSize" Value="18"/> | ||
</Trigger> | ||
<Trigger Property="Grid.Column" Value="0"> | ||
<Setter Property="HorizontalAlignment" Value="Right"/> | ||
</Trigger> | ||
<Trigger Property="Grid.Column" Value="1"> | ||
<Setter Property="Opacity" Value="0.75"/> | ||
<Setter Property="Margin" Value="5,0,0,0"/> | ||
</Trigger> | ||
</Style.Triggers> | ||
</Style> | ||
</Grid.Resources> | ||
<TextBox x:Name="txtFloat" Grid.Row="0" | ||
HorizontalContentAlignment="Right" | ||
FontSize="18" | ||
Margin="0,0,0,5" BorderThickness="0,0,0,2" Background="#00FF7F7F" | ||
TextChanged="OnFloatInput"/> | ||
<TextBlock x:Name="txtInt" Grid.Row="1"/> | ||
<TextBlock x:Name="txtIntHex" Grid.Row="2"/> | ||
<TextBlock Grid.Row="0" Grid.Column="1" Text="(as float)"/> | ||
<TextBlock Grid.Row="1" Grid.Column="1" Text="(as integer in decimal)"/> | ||
<TextBlock Grid.Row="2" Grid.Column="1" Text="(as integer in hex)"/> | ||
</Grid> | ||
</StackPanel> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
/** | ||
* Copyright (C) 2019 Dwscdv3 <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using System; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
using FloatingPointPlayground.UserControls; | ||
|
||
namespace FloatingPointPlayground | ||
{ | ||
/// <summary> | ||
/// MainWindow.xaml 的交互逻辑 | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
private bool disableTextChangedEvent = false; | ||
|
||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
Update(); | ||
} | ||
|
||
private void Update() | ||
{ | ||
var bin = 0; | ||
for (var i = 0; i < 32; i += 1) | ||
{ | ||
bin += ((flippersContainer.Children[31 - i] as BitFlipper).IsChecked == true ? 1 : 0) << i; | ||
} | ||
txtInt.Text = bin.ToString(); | ||
txtIntHex.Text = "0x" + bin.ToString("X8"); | ||
disableTextChangedEvent = true; | ||
txtFloat.Text = BitConverter.ToSingle(BitConverter.GetBytes(bin), 0).ToString(); | ||
disableTextChangedEvent = false; | ||
} | ||
|
||
private void OnBitFlipperClick(object sender, RoutedEventArgs e) => Update(); | ||
|
||
private void OnFloatInput(object sender, TextChangedEventArgs e) | ||
{ | ||
if (!disableTextChangedEvent) | ||
{ | ||
var textBox = sender as TextBox; | ||
|
||
VisualStateManager.GoToElementState(layoutRoot, "Default", true); | ||
|
||
if (float.TryParse(textBox.Text, out var f)) | ||
{ | ||
var bin = BitConverter.ToInt32(BitConverter.GetBytes(f), 0); | ||
for (var i = 0; i < 32; i += 1) | ||
{ | ||
(flippersContainer.Children[i] as BitFlipper).IsChecked = (bin & (1 << (31 - i))) != 0; | ||
} | ||
txtInt.Text = bin.ToString(); | ||
txtIntHex.Text = "0x" + bin.ToString("X8"); | ||
} | ||
else | ||
{ | ||
VisualStateManager.GoToElementState(layoutRoot, "ParseError", true); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.